【CodeForces - 1042C】Array Product(思维,有坑细节)
題干:
You are given an array?aa?consisting of?nn?integers. You can perform the following operations with it:
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
5 5 -2 0 1 -3Output
2 3 1 1 2 1 2 4 1 4 5Input
5 5 2 0 4 0Output
1 3 5 2 5 1 1 2 1 2 4Input
2 2 -1Output
2 2Input
4 0 -10 0 0Output
1 1 2 1 2 3 1 3 4Input
4 0 0 0 0Output
1 1 2 1 2 3 1 3 4Note
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 3Then 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].
題目大意:
n個(gè)數(shù),兩種操作,第一種是a[i]*a[j],刪掉a[i],第二種是直接刪除a[i](其中第二種操作只能用一次)剩下的數(shù)序列號(hào)不變。操作n-1次,使最后剩下的那個(gè)數(shù)最大化。
讓你輸出這n-1次操作(數(shù)字位置的序號(hào)輸出原序列的)。
解題報(bào)告:
分情況,如果全是正數(shù)或者有偶數(shù)個(gè)負(fù)數(shù),那就全乘起來,然后0單獨(dú)處理。
如果有奇數(shù)個(gè)負(fù)數(shù),那就貪心將絕對(duì)值最小的那個(gè)負(fù)數(shù)看成0一類的,然后化為第一種情況,一樣處理。
樣例給的很到位啊,,,不然就忘了判斷那個(gè)操作次數(shù)是否是n-1次了,因?yàn)橹挥?操作不到n-1次時(shí),才用2操作。
再就是x剛開始給了-1e9,,,竟然WA了,,不過想想也是啊,,這個(gè)值還是太大了,,賦初值-1e9-1也行、、
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int pos[MAX],a[MAX]; int tot,cnt; int main() {int n,cur=0;cin>>n;for(int i = 1; i<=n; i++) {scanf("%d",a+i);if(a[i] == 0) pos[++tot] = i;if(a[i] < 0) cnt++;}if(cnt%2==0) {int last = -1;for(int i = 1; i<=n; i++) {if(a[i] == 0) continue;else {if(last == -1) last = i;else printf("1 %d %d\n",last,i),last=i,cur++;}}for(int i = 2; i<=tot; i++) printf("1 %d %d\n",pos[i-1],pos[i]),cur++;if(cur!=n-1) printf("2 %d\n",pos[tot]);}else {int x=-1e9-12,tar;for(int i = 1; i<=n; i++) {if(a[i] >= 0) continue;if(a[i] > x) tar=i,x=a[i];}tot=0;for(int i = 1; i<=n; i++) {if(i==tar||a[i]==0) pos[++tot]=i;}int last = -1;for(int i = 1; i<=n; i++) {if(a[i] == 0 || i == tar) continue;else {if(last == -1) last = i;else printf("1 %d %d\n",last,i),last=i,cur++;}}for(int i = 2; i<=tot; i++) printf("1 %d %d\n",pos[i-1],pos[i]),cur++;if(cur!=n-1) printf("2 %d\n",pos[tot]); }return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 1042C】Array Product(思维,有坑细节)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日本央行成为日本股市的最大买家,我国央行
- 下一篇: 【BZOJ - 4754】独特的树叶(树