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

转载于:https://www.cnblogs.com/yuanhang110/p/11255891.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值