当前位置:   article > 正文

Codeforces 1042 C. Array Product(思维)_vlad was given an array aa of nn positive integers

vlad was given an array aa of nn positive integers. now he wants to build a

                                                                          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

  1. 5
  2. 5 -2 0 1 -3

output

Copy

  1. 2 3
  2. 1 1 2
  3. 1 2 4
  4. 1 4 5

input

Copy

  1. 5
  2. 5 2 0 4 0

output

Copy

  1. 1 3 5
  2. 2 5
  3. 1 1 2
  4. 1 2 4

input

Copy

  1. 2
  2. 2 -1

output

Copy

2 2

input

Copy

  1. 4
  2. 0 -10 0 0

output

Copy

  1. 1 1 2
  2. 1 2 3
  3. 1 3 4

input

Copy

  1. 4
  2. 0 0 0 0

output

Copy

  1. 1 1 2
  2. 1 2 3
  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. 1 5 3
  2. 1 4 2
  3. 1 2 1
  4. 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里面然后一起处理掉就行。

 

四、巨丑的代码

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstdio>
  4. #include<map>
  5. #include<set>
  6. #include<string>
  7. #include<cstring>
  8. #include<algorithm>
  9. #include<queue>
  10. #include<vector>
  11. #include<functional>
  12. using namespace std;
  13. #define inf 0x3f3f3f3f
  14. typedef long long LL;
  15. int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
  16. int n;
  17. int num_zero,pos_zero[200005];
  18. bool vis[200005];
  19. int num_fu,pos_absmin_fu,absmin_fu;
  20. int main()
  21. {
  22. num_zero = num_fu = pos_absmin_fu = 0;
  23. absmin_fu = inf;
  24. scanf("%d", &n);
  25. for (int i = 1; i <= n; i++)
  26. {
  27. int x;
  28. scanf("%d", &x);
  29. if (x < 0)
  30. {
  31. num_fu++;
  32. if (abs(x) < abs(absmin_fu))
  33. {
  34. pos_absmin_fu = i;
  35. absmin_fu = x;
  36. }
  37. }
  38. if (x == 0)
  39. {
  40. pos_zero[num_zero++] = i;
  41. }
  42. }
  43. if (num_fu == 0)
  44. {
  45. if (num_zero == 0)
  46. {
  47. for (int i = 1; i <= n - 1; i++)
  48. printf("1 %d %d\n", i, i + 1);
  49. }
  50. else
  51. {
  52. memset(vis, false, sizeof(vis));
  53. for (int i = 0; i < num_zero - 1; i++)
  54. {
  55. printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
  56. vis[pos_zero[i]] = true;
  57. }
  58. if (num_zero == n)
  59. return 0;
  60. printf("2 %d\n", pos_zero[num_zero - 1]);
  61. vis[pos_zero[num_zero - 1]] = true;
  62. for (int i = 1; i <= n - 1; i++)
  63. {
  64. if (vis[i])continue;
  65. int nx = -1;
  66. for (int j = i+1; j <= n; j++)
  67. {
  68. if (!vis[j])
  69. {
  70. nx = j; break;
  71. }
  72. }
  73. if (nx != -1)
  74. printf("1 %d %d\n", i, nx);
  75. else
  76. break;
  77. i = nx-1;
  78. }
  79. }
  80. }
  81. else if (num_fu & 1)
  82. {
  83. memset(vis, false, sizeof(vis));
  84. if (num_zero == 0)
  85. {
  86. printf("2 %d\n", pos_absmin_fu);
  87. vis[pos_absmin_fu] = true;
  88. }
  89. else
  90. {
  91. printf("1 %d %d\n", pos_absmin_fu, pos_zero[0]);
  92. vis[pos_absmin_fu] = true;
  93. for (int i = 0; i < num_zero - 1; i++)
  94. {
  95. printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
  96. vis[pos_zero[i]] = true;
  97. }
  98. if (num_zero + 1 == n||num_zero==n)
  99. return 0;
  100. printf("2 %d\n", pos_zero[num_zero - 1]);
  101. vis[pos_zero[num_zero - 1]] = true;
  102. }
  103. for (int i = 1; i <= n - 1; i++)
  104. {
  105. if (vis[i])continue;
  106. int nx = -1;
  107. for (int j = i + 1; j <= n; j++)
  108. {
  109. if (!vis[j])
  110. {
  111. nx = j; break;
  112. }
  113. }
  114. if (nx != -1)
  115. printf("1 %d %d\n", i, nx);
  116. else
  117. break;
  118. i = nx - 1;
  119. }
  120. }
  121. else
  122. {
  123. memset(vis, false, sizeof(vis));
  124. for (int i = 0; i < num_zero - 1; i++)
  125. {
  126. printf("1 %d %d\n", pos_zero[i], pos_zero[i + 1]);
  127. vis[pos_zero[i]] = true;
  128. }
  129. if (num_zero == n)
  130. return 0;
  131. if (num_zero >= 1)
  132. {
  133. printf("2 %d\n", pos_zero[num_zero - 1]);
  134. vis[pos_zero[num_zero - 1]] = true;
  135. }
  136. for (int i = 1; i <= n - 1; i++)
  137. {
  138. if (vis[i])continue;
  139. int nx = -1;
  140. for (int j = i + 1; j <= n; j++)
  141. {
  142. if (!vis[j])
  143. {
  144. nx = j; break;
  145. }
  146. }
  147. if (nx != -1)
  148. printf("1 %d %d\n", i, nx);
  149. else
  150. break;
  151. i = nx - 1;
  152. }
  153. }
  154. getchar();
  155. getchar();
  156. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/745367
推荐阅读
相关标签
  

闽ICP备14008679号