http://codeforces.com/problemset/problem/1144/D
You are given an array aa consisting of nn integers. You can perform the following operations arbitrary number of times (possibly, zero):
- Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|ai−aj|ai:=ai+|ai−aj| ;
- Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai−|ai−aj|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 (1≤n≤2⋅1051≤n≤2⋅105 ) — the number of elements in aa .
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤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 (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 1≤ip,jp≤n1≤ip,jp≤n , |ip−jp|=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
5 2 4 6 6 6
Output
2 1 2 3 1 1 2
Input
3 2 8 10
Output
2 2 2 1 2 3 2
Input
4 1 1 1 1
Output
0
题目大意:给一个序列,通过两种操作,最后使得序列的所有数都相等,求最少的操作次数。
思路: 两种操作分ai>aj和ai<aj两种情况进行化简,就会发现两种操作可以让一个数与其相邻的数相等。(看ai 和aj的大小 决定使用哪种操作)那么问题就简单了,手边遍历一遍求出众数,并存储其位置,然后扫描一遍就行了。(利用存储的位置 向左或向右扫描)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int a[200005];
int vis[200005];
vector<int> vec;
int MAX=0;
int n;
int main()
{
int v;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(++vis[a[i]]>MAX)
{
MAX=vis[a[i]];
v=a[i];
}
}
if(MAX==n)
printf("0\n");
else
{
printf("%d\n",n-MAX);
for(int i=1;i<=n;i++)
if(a[i]==v)
vec.push_back(i);
int b=vec[0]-1;
while(b>=1)
{
if(a[b]>v)
printf("2 %d %d\n",b,b+1);
else
printf("1 %d %d\n",b,b+1);
--b;
}
for(int i=1;i<vec.size();i++)
{
for(int j=vec[i]-1;j>vec[i-1];j--)
{
if(a[j]>v)
printf("2 %d %d\n",j,j+1);
else
printf("1 %d %d\n",j,j+1);
}
}
b=vec[vec.size()-1]+1;
while(b<=n)
{
if(a[b]>v)
printf("2 %d %d\n",b,b-1);
else
printf("1 %d %d\n",b,b-1);
++b;
}
}
return 0;
}