1. 程式人生 > >[codeforces 1042C]Array Product

[codeforces 1042C]Array Product

time limit per test:1 second memory limit per test:256 megabytes

You are given an array aconsisting of nn integers. You can perform the following operations with it:

Choose some positions ii and j(1i,jn,ij)j (1≤i,j≤n,i≠j), write the value of ai⋅aj into the j-th cell and remove the number from the i-th cell; Choose some position iand remove the number from the i

i-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 n(2n2105)n(2≤n≤2⋅105) — the number of elements in the array.

The second line contains nnintegers a1,a2,,an(109ai109)a_1,a_2,…,a_n (−10^9≤a_i≤10^9) — the elements of the array.

Output

Print n1n−1 lines. The kk-th line should contain one of the two possible operations.

The operation of the first type should look like this: 11 iki_k jkj_k, where 11 is the type of operation, iki_k and jkj_k are the positions of the chosen elements.

The operation of the second type should look like this: 22 ikik, where 2 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.

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 XX 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][X,X,X,10,3][X,X,X,X,30][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]. Thus, the maximum answer is 30. 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][X,X,X,40,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]

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][40,X,X,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]

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]

題意: 給一個長度為nn的序列,有兩種操作: 11 ii jj :將ii位置上的數字刪除(給ii位置填一個X),然後將jj位置上的數字變為aiaja_i*a_j 22 ii :將ii位置上的數字刪除(給ii位置填一個X),這個操作最多做一次

要求做正好n1n-1次操作,讓最後剩下來的數字最大。

題解: 考慮沒有0的情況: 1.如果有奇數個負數,那麼就刪除最大的負數,然後其他都全部通過操作11乘到同一個位置就好了。 2.如果有偶數個負數,那麼久所有的數字乘起來就好了。 考慮有0的情況: 先將所有的0乘到同一個位置。 1.如果有奇數個負數,那麼將最大的負數乘到0上,然後刪掉0,然後其他全部數字通過操作11乘到同一個位置。 2.如果有偶數個負數,先刪掉0,然後其他全部數字通過操作11乘到同一個位置。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define pa pair<int,int>
using namespace std;
vector<pa>fa,fb,f;
int n,a[200004];
bool vis[200004];
int w33ha(){
    memset(vis,0,sizeof(vis));
    fa.clear();
    fb.clear();
    f.clear();
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]==0){
            f.push_back(make_pair(a[i],i));
        }
        if(a[i]<0){
            fb.push_back(make_pair(a[i],i));
        }
        if(a[i]>0){
            fa.push_back(make_pair(a[i],i));
        }
    }
    sort(fb.begin(),fb.end());
    sort(fa.begin(),fa.end());
    if(f.size()==0){
        if(fb.size()&1){
            printf("2 %d\n",fb[fb.size()-1].second);
            int bg=-1,gt=fb[fb.size()-1].second;
            for(int i=1;i<=n;i++){
                if(i!=gt){
                    bg=i;
                }
            }
            if(bg==-1)return 0;
            for(int i=1;i<=n;i++){
                if(i!=gt&&bg!=i){
                    printf("1 %d %d\n",i,bg);
                }
            }
            return 0;
        }
        else{
            for(int i=2;i<=n;i++){
                printf("1 %d 1\n",i,1);
            }
            return 0;
        }
    }
    else{
        if(fb.size()&1){
            int cnt=0;
            for(int i=1;i<f.size();i++){
                printf("1 %d %d\n",f[i].second,f[0].second);
                ++cnt;
                vis[f[i].second]=1;
            }
            ++cnt;
            printf("1 %d %d\n",fb[fb.size()-1].second,f[0].second);
            vis[fb[fb.size()-1].second]=1;
            if(cnt<n-1)printf("2 %d\n",f[0].second);
            else return 0;
            vis[f[0].second]=1;
            int ge=-1;
            for(int i=1;i<=n;i++){
                if(!vis[i]){ge=i;break;}
            }
            for(int i=1;i<=n;i++){
                if(!vis[i]&&i!=ge)printf("1 %d %d\n",i,ge);
            }
            return 0;
        }
        else{
            int cnt=0;
            for(int i=1;i<f.size();i++){
                printf("1 %d %d\n",f[i].second,f[0].second);
                ++cnt;
                vis[f[i].second]=1;
            }

            if(cnt<n-1)printf("2 %d\n",f[0].second);
            else return 0;
            ++cnt;
            vis[f[0].second]=1;
            int ge=-1;
            for(int i=1;i<=n;i++){