【CF1012A】 Photo of The Sky

题目

题意翻译
给你一个长度为nn的整数序列,你可以对其做两种操作:

11、选0<i,j<=n,i!=j0<i,j<=n,i!=j,将a_ja
j
​ 替换为a_i\cdot a_ja
i
​ ⋅a
j
​ ,删除a_ia
i
​ 。

22、选一个未被删除的a_ia
i
​ ,将其删除。该操作在任意时刻均可执行,最多执行一次。

你需要操作n-1次,剩下一个数,使其最大。由于剩下的数可能会很大,你需要输出得到它的操作序列。

输出任意能得到最大数的操作序列均可。

题目描述
You are given an array a a consisting of n n integers. You can perform the following operations with it:

Choose some positions i i and j j ( 1 \le i, j \le n, i \ne j 1≤i,j≤n,i≠j ), write the value of a_i \cdot a_j a
i
​ ⋅a
j
​ into the j j -th cell and remove the number from the i i -th cell;
Choose some position i i and 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 - 1 n−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.

输入输出格式
输入格式:
The first line contains a single integer n n ( 2 \le n \le 2 \cdot 10^5 2≤n≤2⋅10
5
) — the number of elements in the array.

The second line contains n n integers a_1, a_2, \dots, a_n a
1
​ ,a
2
​ ,…,a
n
​ ( -10^9 \le a_i \le 10^9 −10
9
≤a
i
​ ≤10
9
) — the elements of the array.

输出格式:
Print n - 1 n−1 lines. The k k -th line should contain one of the two possible operations.

The operation of the first type should look like this: 1~ i_k~ j_k 1 i
k
​ j
k
​ , where 1 1 is the type of operation, i_k i
k
​ and j_k j
k
​ are the positions of the chosen elements.

The operation of the second type should look like this: 2~ i_k 2 i
k
​ , where 2 2 is the type of operation, i_k i
k
​ 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.

输入输出样例
输入样例#1: 复制
5
5 -2 0 1 -3
输出样例#1: 复制
2 3
1 1 2
1 2 4
1 4 5
输入样例#2: 复制
5
5 2 0 4 0
输出样例#2: 复制
1 3 5
2 5
1 1 2
1 2 4
输入样例#3: 复制
2
2 -1
输出样例#3: 复制
2 2
输入样例#4: 复制
4
0 -10 0 0
输出样例#4: 复制
1 1 2
1 2 3
1 3 4
输入样例#5: 复制
4
0 0 0 0
输出样例#5: 复制
1 1 2
1 2 3
1 3 4
说明
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] \to [5, -2, X, 1, -3] \to [X, -10, X, 1, -3] \to [5,−2,0,1,−3]→[5,−2,X,1,−3]→[X,−10,X,1,−3]→ [X, X, X, -10, -3] \to [X, X, X, X, 30] [X,X,X,−10,−3]→[X,X,X,X,30] . Thus, the maximum answer is 30 30 . Note, that other sequences that lead to the answer 30 30 are also correct.

The second example has, for example, the following sequence of transformations of the array: [5, 2, 0, 4, 0] \to [5, 2, X, 4, 0] \to [5, 2, X, 4, X] \to [X, 10, X, 4, X] \to [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] \to [5, 2, 0, 4, X] \to [5, 8, 0, X, X] \to [40, X, 0, X, X] \to [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] \to [2, X] [2,−1]→[2,X] .

The fourth example can have the following sequence of transformations of the array: [0, -10, 0, 0] \to [X, 0, 0, 0] \to [X, X, 0, 0] \to [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] \to [X, 0, 0, 0] \to [X, X, 0, 0] \to [X, X, X, 0] [0,0,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0] .

思路

我们可以想到我们需要先把所有的0,合为一个,然后判断负数两两配对是否多出一个(!%2),两两配对后,两个负数相乘变为正数,正数当然越乘越大。

如果多出一个来,因为要让乘积最大,那么对于负数而言,我们需要删掉最大的负数,将这个最大的负数和0进行1操作,然后删除0(删除两个数的乘积)。

如果负数为偶数,那么我们直接把0的位置,删掉就好了,将剩下的的乘起来。

这样大体的思想就出来了,要注意一大堆细节问题:

1.如果全是0,那么我们只进行1操作(n-1)次就好,做完return 0;

2.没有0,。

3.没有负数。

4.没有正数。

上边的四种情况在代码中都有体现:

(情况分开讨论比较好理解,可能代码有点长)

代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int cnt,cnt1,cnt2,n,x;
struct ahah
{
    int x,pos;
} a[200006],b[200006],c[200006];
bool cmp(ahah a,ahah b){ return abs(a.x)<abs(b.x); }
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&x);
        if(x==0)c[++cnt].pos=i;
        if(x<0)a[++cnt1].x=x,a[cnt1].pos=i;
        if(x>0)b[++cnt2].x=x,b[cnt2].pos=i;
    }
    sort(a+1,a+1+cnt1,cmp);
    sort(b+1,b+1+cnt2,cmp);
    int v=-1;
    if(cnt!=0)
    {
        v=c[1].pos;
        for(int i=2; i<=cnt; i++)
        {
            printf("1 %d %d\n",v,c[i].pos);
            v=c[i].pos;
        }
    }
    if(cnt==n)return 0;
    if(cnt1%2==0)
    {
        if(v!=-1)printf("2 %d\n",v),v=-1;
        if(cnt1!=0)
        {
            if(v==-1)v=a[1].pos;
            else printf("1 %d %d\n",v,a[1].pos),v=a[1].pos;
            for(int i=2; i<=cnt1; i++)
            {
                printf("1 %d %d\n",v,a[i].pos);
                v=a[i].pos;
            }
        }
        if(cnt2!=0)
        {
            if(v==-1)v=b[1].pos;
            else printf("1 %d %d\n",v,b[1].pos),v=b[1].pos;
            for(int i=2; i<=cnt2; i++)
            {
                printf("1 %d %d\n",v,b[i].pos);
                v=b[i].pos;
            }
        }
    }
    else
    {
        if(v==-1)v=a[1].pos;
        else printf("1 %d %d\n",v,a[1].pos),v=a[1].pos;
        if(cnt+1!=n)printf("2 %d\n",v),v=-1;
        else return 0;
        if(cnt1>1)
        {
            v=a[2].pos;
            for(int i=3; i<=cnt1; i++)
            {
                printf("1 %d %d\n",v,a[i].pos);
                v=a[i].pos;
            }
        }
        if(cnt2>0)
        {
            if(v==-1)v=b[1].pos;
            else printf("1 %d %d\n",v,b[1].pos),v=b[1].pos;
            for(int i=2; i<=cnt2; i++)
            {
                printf("1 %d %d\n",v,b[i].pos);
                v=b[i].pos;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值