Codeforces Round #550 (Div. 3) D. Equalize Them All

D. Equalize Them All
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
Copy
5
2 4 6 6 6
output
Copy
2
1 2 3 
1 1 2 
input
Copy
3
2 8 10
output
Copy
2
2 2 1 
2 3 2 
input
Copy
4
1 1 1 1
output
Copy
0

题意为有两种操作,一种是把比自己小的相邻数字改成自己,另一种是把一个比自己大的相邻数字改成自己,问把数组中所有数字改成相同大小最少需要几次并输出操作顺序。
方法其实不困难,找到出现最多次数的那个数字,然后以它为中心,进行操作,然后边操作边输出操作方法,注意不要排序,不然会改变顺序。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int INF=1e9+5;
typedef pair<int,int> pii;
int n,k;
int vis[maxn];
int a[maxn];

int main()
{
    cin>>n;
    int mx=-1,m=0;
    for(int i=1; i<=n; i++)
    {
        int x;
        cin>>x;
        a[i]=x;
        vis[x]++;
        if(mx<vis[x])
        {
            mx=vis[x];
            m=x;
        }
    }
    int p;
    for(int i=1; i<=n; i++)
    {
        if(a[i]==m)
        {
            p=i;
            break;
        }
    }
    cout<<n-mx<<endl;

    for(int i=p; i>=1; i--)
    {
        //cout<<i<<endl;
        if(a[i]<m)
            cout<<1<<" "<<i<<" "<<i+1<<endl;
        else if(a[i]>m)
            cout<<2<<" "<<i<<" "<<i+1<<endl;
    }
    for(int i=p; i<=n; i++)
    {
        if(a[i]<m)
            cout<<1<<" "<<i<<" "<<i-1<<endl;
        else if(a[i]>m)
            cout<<2<<" "<<i<<" "<<i-1<<endl;
    }
    return 0;
}
View Code
posted @ 2019-04-03 18:33  yosoro  阅读(146)  评论(0编辑  收藏  举报