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

转载于:https://www.cnblogs.com/youchandaisuki/p/10650755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值