[CF]Equalize Them All

D. Equalize Them All

Description

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 10181018by 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 aiaiis 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 (11 means 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 10181018by absolute value.

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

正确解法:

题目保证最后数列一定会化成一样的。我们看前两个选择,其实就是把 ai 化成 aj。

其中的不同就是 一个 ai < aj 另一个 ai > aj

那么我们找最多的那一个数字就是最小的次数

剩下的就是一个个划嘛,从最多的那一个数字的第一个开始往前化,往后化。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <vector>
 9 using namespace std;
10 typedef long long ll;
11 const int inf=0x7fffffff;
12 const int N=200000+100;
13 int n,a[N],cnt[N],pos,maxx=0,xx;
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i=1;i<=n;i++)
18     {
19         scanf("%d",&a[i]);
20         cnt[a[i]]++;
21         if(cnt[a[i]]>maxx)
22         {
23             maxx=cnt[a[i]];
24             xx=a[i];
25         }
26     }
27     for(int i=1;i<=n;i++)
28         if(a[i]==xx)
29         {
30         pos=i;
31         break;
32         }
33     cout<<n-maxx<<endl;
34     for(int i=pos-1;i>=1;i--)
35     {
36         if(a[i]<a[i+1])
37         {
38             cout<<1<<" "<<i<<" "<<i+1<<endl;
39         }
40         else
41         {
42             cout<<2<<" "<<i<<" "<<i+1<<endl;
43         }
44         a[i]=a[i+1];
45     }
46     for(int i=1;i<=n;i++)
47         if(a[i-1]==xx&&a[i]!=xx)
48         {
49             if(a[i]>a[i-1])
50                 cout<<2<<" ";
51             else
52                 cout<<1<<" ";
53             cout<<i<<" "<<i-1<<endl;
54             a[i]=xx;
55         }
56 
57 
58     return 0;
59 }
View Code

猜你喜欢

转载自www.cnblogs.com/Kaike/p/10716131.html
ALL