Codeforces Round #550 (Div. 3) D. Equalize Them All(思维)

 

题目链接:https://codeforces.com/contest/1144/problem/D

       题意是给了n个数,对于ai有两种操作,一种是ai = ai + |ai - aj|,另一种是ai = ai - |ai - aj|(i和j必须相邻),问至少多少次操作后能使这n个数相同,输出最少的操作数,以及具体的操作。

       首先很容易想到对于一次操作来说可以使两个不同的数变为相同的。所以最少的操作次数其实就是n减去出现最频繁的数的个数,然后对于剩下的数让它都变为出现最频繁的数就好了。只需要找到那个数,然后向左向右扫一遍就好了。


AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
ll pre[200005];
map<ll,int> ma;

int main()
{
  scanf("%d",&n);
  int ans = 0;
  int pos, pos2;
  for(int i=1;i<=n;i++){
    scanf("%lld", &pre[i]);
    ma[pre[i]] ++;
    if(ans <= ma[pre[i]]){
      ans = ma[pre[i]];
      pos = pre[i];
      pos2 = i;
    }
  }
  printf("%d\n", n - ans);
  if(ans == n) return 0;
  int p = pos2 + 1;
  while(p <= n){
    if(pre[p] != pos){
      if(pre[p] < pos){
        cout<<"1 "<<p<<" "<<p-1<<endl;
      }
      else{
        cout<<"2 "<<p<<" "<<p-1<<endl;
      }
    }
    p ++;
  }
  p = pos2 - 1;
  while(p >= 1){
    if(pre[p] != pos){
      if(pre[p] < pos){
        cout<<"1 "<<p<<" "<<p+1<<endl;
      }
      else{
        cout<<"2 "<<p<<" "<<p+1<<endl;
      }
    }
    p--;
  }
  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值