Array Product(CodeForces
You are given an array aa consisting of nn integers. You can perform the following operations with it:
- Choose some positions ii and jj (1≤i,j≤n,i≠j1≤i,j≤n,i≠j), write the value of ai⋅ajai⋅aj into the jj-th cell and remove the number from the ii-th cell;
- Choose some position ii and remove the number from the ii-th cell (this operation can be performed no more than once and at any point of time, not necessarily in the beginning).
The number of elements decreases by one after each operation. However, the indexing of positions stays the same. Deleted numbers can't be used in the later operations.
Your task is to perform exactly n−1n−1 operations with the array in such a way that the only number that remains in the array is maximum possible. This number can be rather large, so instead of printing it you need to print any sequence of operations which leads to this maximum number. Read the output format to understand what exactly you need to print.
Input
The first line contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of the array.
Output
Print n−1n−1 lines. The kk-th line should contain one of the two possible operations.
The operation of the first type should look like this: 1 ik jk1 ik jk, where 11 is the type of operation, ikik and jkjk are the positions of the chosen elements.
The operation of the second type should look like this: 2 ik2 ik, where 22 is the type of operation, ikik is the position of the chosen element. Note that there should be no more than one such operation.
If there are multiple possible sequences of operations leading to the maximum number — print any of them.
Examples
Input
5 5 -2 0 1 -3
Output
2 3 1 1 2 1 2 4 1 4 5
Input
5 5 2 0 4 0
Output
1 3 5 2 5 1 1 2 1 2 4
Input
2 2 -1
Output
2 2
Input
4 0 -10 0 0
Output
1 1 2 1 2 3 1 3 4
Input
4 0 0 0 0
Output
1 1 2 1 2 3 1 3 4
Note
Let X be the removed number in the array. Let's take a look at all the examples:
The first example has, for example, the following sequence of transformations of the array: [5,−2,0,1,−3]→[5,−2,X,1,−3]→[X,−10,X,1,−3]→[5,−2,0,1,−3]→[5,−2,X,1,−3]→[X,−10,X,1,−3]→ [X,X,X,−10,−3]→[X,X,X,X,30][X,X,X,−10,−3]→[X,X,X,X,30]. Thus, the maximum answer is 3030. Note, that other sequences that lead to the answer 3030 are also correct.
The second example has, for example, the following sequence of transformations of the array: [5,2,0,4,0]→[5,2,X,4,0]→[5,2,X,4,X]→[X,10,X,4,X]→[5,2,0,4,0]→[5,2,X,4,0]→[5,2,X,4,X]→[X,10,X,4,X]→ [X,X,X,40,X][X,X,X,40,X]. The following answer is also allowed:
1 5 3 1 4 2 1 2 1 2 3
Then the sequence of transformations of the array will look like this: [5,2,0,4,0]→[5,2,0,4,X]→[5,8,0,X,X]→[40,X,0,X,X]→[5,2,0,4,0]→[5,2,0,4,X]→[5,8,0,X,X]→[40,X,0,X,X]→ [40,X,X,X,X][40,X,X,X,X].
The third example can have the following sequence of transformations of the array: [2,−1]→[2,X][2,−1]→[2,X].
The fourth example can have the following sequence of transformations of the array: [0,−10,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0][0,−10,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0].
The fifth example can have the following sequence of transformations of the array: [0,0,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0][0,0,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0].
程式碼如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;
int a[222222];
int z[222222];
int f[222222];
int main()
{
int n;
scanf("%d",&n);
int k=0,kk=0,minn=INF,nn=0;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
z[k++]=i;
if(a[i]<0)
{
f[kk++]=i;
if(minn>-1*a[i])
{
minn=-1*a[i];
nn=i;
}
}
}
if(kk%2)//如果為奇數個小於0的值,去掉最大的一個負數,其餘數所乘最大
{
a[nn]=0;//任何數*0=0
z[k++]=nn;
}
int t=0;
for(int i=0;i<n;i++)//找第一個可以輸出的值
{
if(a[i]!=0)
{
t=i;
break;
}
}
sort(z,z+k);
for(int i=0;i<k-1;i++)//處理0,用1的原因是考慮資料全為0的情況
cout<<"1 "<<z[i]+1<<" "<<z[i+1]+1<<endl;
if(k!=0&&k!=n)//考慮資料全為0的情況
cout<<"2 "<<z[k-1]+1<<endl;
for(int i=t+1;i<n;i++)
{
if(a[i]!=0)
{
cout<<"1 "<<t+1<<" "<<i+1<<endl;
t=i;
}
}
return 0;
}