C. Array Product
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
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
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].
一、原题地址
点我传送
二、大致题意
现在有两种操作。
1、将数组中对应 i 和 j 位置上的数ai 、aj相乘得到 ai*aj ,把得到的值放在 j 的位置上。(对应的 i 位置则被挖空了)。
2、直接移除位置 i 上的数。且该操作最多只能进行一次(不是必须进行一次)。
现在询问经过n-1次操作之后剩下的一个数,最大值能是多少。
三、大致思路
这堆数做的是相乘的操作,所以进行操作1的时候他们的操作顺序是无所谓的。
想要得到的数最大,要考虑的只是这堆数中0的个数和负数的个数。再一看题目给出的样例,实际上情况数是很少的。
1、当数组中没有0和负数时,必定是所有数一个一个乘过去。
2、如果存在零,那么其实只需要把这些零先用操作1集合在一起,然后一次性移除。
3、若又存在零又存在负数,那么就需要考虑负数的奇偶。
若为偶数,那么情况其实和第二种做法是一样的。
若为奇数,那么我们在读入的时候顺路找出一个绝对值最小的负数,记录下这个数的位置。然后把这个数乘到0里面然后一起处理掉就行。
四、巨丑的代码
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
int n;
int num_zero,pos_zero[200005];
bool vis[200005];
int num_fu,pos_absmin_fu,absmin_fu;
int main()
{
num_zero = num_fu = pos_absmin_fu = 0;
absmin_fu = inf;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
if (x < 0)
{
num_fu++;
if (abs(x) < abs(absmin_fu))
{
pos_absmin_fu = i;
absmin_fu = x;
}
}
if (x == 0)
{
pos_zero[num_zero++] = i;
}
}
if (num_fu == 0)
{
if (num_zero == 0)
{
for (int i = 1; i <= n - 1; i++)
printf("1 %d %d\n", i, i + 1);
}
else
{
memset(vis, false, sizeof(vis));
for (int i = 0; i < num_zero - 1; i++)
{
printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
vis[pos_zero[i]] = true;
}
if (num_zero == n)
return 0;
printf("2 %d\n", pos_zero[num_zero - 1]);
vis[pos_zero[num_zero - 1]] = true;
for (int i = 1; i <= n - 1; i++)
{
if (vis[i])continue;
int nx = -1;
for (int j = i+1; j <= n; j++)
{
if (!vis[j])
{
nx = j; break;
}
}
if (nx != -1)
printf("1 %d %d\n", i, nx);
else
break;
i = nx-1;
}
}
}
else if (num_fu & 1)
{
memset(vis, false, sizeof(vis));
if (num_zero == 0)
{
printf("2 %d\n", pos_absmin_fu);
vis[pos_absmin_fu] = true;
}
else
{
printf("1 %d %d\n", pos_absmin_fu, pos_zero[0]);
vis[pos_absmin_fu] = true;
for (int i = 0; i < num_zero - 1; i++)
{
printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
vis[pos_zero[i]] = true;
}
if (num_zero + 1 == n||num_zero==n)
return 0;
printf("2 %d\n", pos_zero[num_zero - 1]);
vis[pos_zero[num_zero - 1]] = true;
}
for (int i = 1; i <= n - 1; i++)
{
if (vis[i])continue;
int nx = -1;
for (int j = i + 1; j <= n; j++)
{
if (!vis[j])
{
nx = j; break;
}
}
if (nx != -1)
printf("1 %d %d\n", i, nx);
else
break;
i = nx - 1;
}
}
else
{
memset(vis, false, sizeof(vis));
for (int i = 0; i < num_zero - 1; i++)
{
printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
vis[pos_zero[i]] = true;
}
if (num_zero == n)
return 0;
if (num_zero >= 1)
{
printf("2 %d\n", pos_zero[num_zero - 1]);
vis[pos_zero[num_zero - 1]] = true;
}
for (int i = 1; i <= n - 1; i++)
{
if (vis[i])continue;
int nx = -1;
for (int j = i + 1; j <= n; j++)
{
if (!vis[j])
{
nx = j; break;
}
}
if (nx != -1)
printf("1 %d %d\n", i, nx);
else
break;
i = nx - 1;
}
}
getchar();
getchar();
}