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;
}

 

全部评论

相关推荐

12-20 11:26
复旦大学 Java
点赞 评论 收藏
分享
牛马人的牛马人生:500一天吗?香麻了
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务