题目链接: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;
}