1. 程式人生 > >Codeforces Round #510 (Div. 2) C. Array Product

Codeforces Round #510 (Div. 2) C. Array Product

C. Array Product

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

  1. 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;
  2. 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

Copy

5
5 -2 0 1 -3

output

Copy

2 3
1 1 2
1 2 4
1 4 5

input

Copy

5
5 2 0 4 0

output

Copy

1 3 5
2 5
1 1 2
1 2 4

input

Copy

2
2 -1

output

Copy

2 2

input

Copy

4
0 -10 0 0

output

Copy

1 1 2
1 2 3
1 3 4

input

Copy

4
0 0 0 0

output

Copy

1 1 2
1 2 3
1 3 4

題意:給你n個整數,你有兩個操作:

1.a[j]=a[i]*a[j],同時刪去a[i],i和j均為任選。

2.刪去任意一個數字,整個陣列只允許刪一次。

思路:

我們可以分析出來四種情況:

1.沒有0,沒有負數:此時從大到小,令兩數字累乘即可。

2.    有0,沒有負數:首先將所有0累乘到一起,變成一個0,接著刪除一下0,即刻變成情況1.

3.    沒有0,有負數:負數個數分奇偶,偶數時負號無視,按照情況1。奇數時,刪去一個絕對值最小的負數,剩餘按照情況1。

4.       有0,有負數:負數個數分奇偶,偶數時負號無視,按照情況2,。奇數時,將絕對值最小的一個與所有的0累乘,此時剩下偶數個負數,和一個0,刪去0,無視負號,即情況1。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
    ll x,id;
}a[200020];
bool cmp(node p1,node p2)
{
    return p1.x>p2.x;
}
ll n;
int main()
{
    while(~scanf("%lld",&n))
    {
        ll ok1=0,ok2=0;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].x);
            a[i].id=i;
            if(a[i].x==0)ok1=1;   //有0
            if(a[i].x<0)ok2=1;    //有負數
        }
        sort(a+1,a+n+1,cmp);
        if(ok1==0&&ok2==0)     //有0沒負數   
        {
            for(ll i=2;i<=n;i++)
            {
                printf("1 %lld %lld\n",a[i-1].id,a[i].id);
            }
        }
        else if(ok1==1&&ok2==0)
        {
            ll pos=0;
            for(ll i=n;i>=1;i--)
            {
                if(a[i].x!=0)
                {
                    pos=i;
                    break;
                }
            }
            for(ll i=2;i<=pos;i++)
            {
                printf("1 %lld %lld\n",a[i-1].id,a[i].id);
            }
            for(ll i=pos+2;i<=n;i++)
            {
                printf("1 %lld %lld\n",a[i-1].id,a[i].id);
            }
            if(pos!=0)printf("2 %lld\n",a[n].id);
        }
        else if(ok1==0&&ok2==1)
        {
            ll c=0;
            for(ll i=1;i<=n;i++)
            {
                if(a[i].x<0)c++;
            }
            if(c&1)
            {
                ll p=0;
                for(ll i=1;i<=n;i++)
                {
                    if(a[i].x<0)
                    {
                        if(p==0)
                        {
                            a[i].x=0;
                            printf("2 %lld\n",a[i].id);
                            p=1;
                        }
                        else a[i].x=-a[i].x;
                    }
                }
                sort(a+1,a+n+1,cmp);
                for(ll i=2;i<n;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
            }
            else
            {
                for(ll i=1;i<=n;i++)
                {
                    if(a[i].x<0)
                    {
                        a[i].x=-a[i].x;
                    }
                }
                sort(a+1,a+n+1,cmp);
                for(ll i=2;i<=n;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
            }
        }
        else
        {
            ll c=0;
            for(ll i=1;i<=n;i++)
            {
                if(a[i].x<0)c++;
            }
            if(c&1)
            {
                ll p=0;
                for(ll i=1;i<=n;i++)
                {
                    if(a[i].x<0)
                    {
                        if(p==0)
                        {
                            a[i].x=0;
                            p=1;
                        }
                        else a[i].x=-a[i].x;
                    }
                }
                sort(a+1,a+n+1,cmp);
                ll pos=0;
                for(ll i=n;i>=1;i--)
                {
                    if(a[i].x!=0)
                    {
                        pos=i;
                        break;
                    }
                }
                for(ll i=2;i<=pos;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
                for(ll i=pos+2;i<=n;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
                if(pos!=0)printf("2 %lld\n",a[n].id);
            }
            else
            {
                for(ll i=1;i<=n;i++)
                {
                    if(a[i].x<0)
                    {
                        a[i].x=-a[i].x;
                    }
                }
                sort(a+1,a+n+1,cmp);
                ll pos=0;
                for(ll i=n;i>=1;i--)
                {
                    if(a[i].x!=0)
                    {
                        pos=i;
                        break;
                    }
                }
                for(ll i=2;i<=pos;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
                for(ll i=pos+2;i<=n;i++)
                {
                    printf("1 %lld %lld\n",a[i-1].id,a[i].id);
                }
                printf("2 %lld\n",a[n].id);
            }
        }
    }
    return 0;
}