Codeforces1144D(D题)Equalize Them All

D. Equalize Them All

You are given an array aa consisting of nn integers. You can perform the following operations arbitrary number of times (possibly, zero):

  1. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|aiaj|ai:=ai+|ai−aj|;
  2. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai|aiaj|ai:=ai−|ai−aj|.

The value |x||x| means the absolute value of xx. For example, |4|=4|4|=4, |3|=3|−3|=3.

Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.

It is guaranteed that you always can obtain the array of equal elements using such operations.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

Input

The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai21050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

In the first line print one integer kk — the minimum number of operations required to obtain the array of equal elements.

In the next kk lines print operations itself. The pp-th operation should be printed as a triple of integers (tp,ip,jp)(tp,ip,jp), where tptp is either 11 or 22 (11means that you perform the operation of the first type, and 22 means that you perform the operation of the second type), and ipip and jpjp are indices of adjacent elements of the array such that 1ip,jpn1≤ip,jp≤n, |ipjp|=1|ip−jp|=1. See the examples for better understanding.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

If there are many possible answers, you can print any.

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int n,a[200010],cnt[200010],mx=-1,mxd;
 5 int main() {
 6     cin>>n;
 7     for(int i=1; i<=n; i++) {
 8         cin>>a[i];
 9         cnt[a[i]]++;
10     }
11     for(int i=0; i<=200000; i++) 
12         if(cnt[i]>mx) {
13             mx=cnt[i];
14             mxd=i;
15         }
16     cout<<n-mx<<endl;
17     for(int i=2; i<=n; i++) { 
18         if(a[i-1]!=mxd)continue;
19         if(a[i]>a[i-1])cout<<2<<" "<<i<<" "<<i-1<<endl;
20         if(a[i]<a[i-1])cout<<1<<" "<<i<<" "<<i-1<<endl;
21         a[i]=a[i-1];
22     }
23     for(int i=n-1; i>=1; i--) { 
24         if(a[i+1]!=mxd)continue;
25         if(a[i]>a[i+1])cout<<2<<" "<<i<<" "<<i+1<<endl;
26         if(a[i]<a[i+1])cout<<1<<" "<<i<<" "<<i+1<<endl;
27         a[i]=a[i+1];
28     }
29     return 0;
30 }

思路分析:先找出最多次数的数字,然后向左向右循环比较,向左时,如果左元素小,通过2方式来变为出现最多次数字。如果左元素大,通过1方式来变为出现最多次数字。向右同理。

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

posted @ 2019-07-27 17:19  远航。  阅读(274)  评论(0编辑  收藏  举报