首页 > 其他 > 详细

[CF]Equalize Them All

时间:2019-04-16 12:43:17      阅读:195      评论:0      收藏:0      [点我收藏+]

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

 

 

 

 

[CF]Equalize Them All

原文:https://www.cnblogs.com/Kaike/p/10716131.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!