You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
- Choose a pair of indices (i,j)such that |i−j|=1 (indices i and j are adjacent) and set ai:=ai+|ai−aj|;
- Choose a pair of indices (i,j)such that |i−j|=1 (indices i and j are adjacent) and set ai:=ai−|ai−aj|.
The value |x|means the absolute value of x. For example, |4|=4, |−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 1018 by absolute value.
Input
The first line of the input contains one integer n
(1≤n≤2⋅105) — the number of elements in a.
The second line of the input contains n integers a1,a2,…,an (0≤ai≤2⋅105), where ai is the i-th element of a
.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (tp,ip,jp), where tp is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and ip and jp are indices of adjacent elements of the array such that 1≤ip,jp≤n, |ip−jp|=1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 1018by 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 <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define maxn 200005
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Opera{
int index;
int i,j;
};
int store[maxn];
Opera opera[maxn*2];
map<int,int> mapping;
inline ll cal(int index,int i,int j);
int main(){
int N;
scanf("%d",&N);
int sup=INT_MIN,equ;
rep(i,1,N+1){
scanf("%d",&store[i]);
mapping[store[i]]++;
if(mapping[store[i]]>sup){
sup=mapping[store[i]];
equ=store[i];
}
}
int t=0,pos=1;
while(pos<=N){
while(pos<=N && store[pos]!=equ){
pos++;
}
if(pos==N+1)
break;
drep(i,pos-1,1){
if(store[i]==equ)
break;
if(equ<store[i]){
opera[t].index=2;
}else{
opera[t].index=1;
}
opera[t].i=i,opera[t].j=i+1;
t++;
}
pos++;
}
pos--;
while(store[pos]!=equ)
pos--;
rep(i,pos+1,N+1){
if(equ<store[i]){
opera[t].index=2;
}else
opera[t].index=1;
opera[t].i=i,opera[t].j=i-1;
t++;
}
printf("%d\n",t);
rep(i,0,t){
store[opera[i].i]=cal(opera[i].index,opera[i].i,opera[i].j);
printf("%d %d %d\n",opera[i].index,opera[i].i,opera[i].j);
}
re 0;
}
inline ll cal(int index,int i,int j){
switch(index){
case 1:
re store[i]+abs(store[i]-store[j]);
case 2:
re store[i]-abs(store[i]-store[j]);
}
}