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

探讨了在给定数组中,通过特定操作使最终数组值达到最大的策略。操作包括选择两个元素相乘并替换其中一个,或仅删除一个元素。文章分析了不同情况下的最优策略,如存在零、正数和负数时的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值