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

Codeforces Round #510 (Div. 2) /1042 C. Array Product(思維)

                                                                          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:

  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

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