CF1042C  Array Product 分类讨论+贪心-LMLPHP

考虑有无负数(负数的个数为奇视作“有”,否则为“无”)和有无零

无负数无零,全部合并即可

无负数有零,那么把零合并起来,删掉零

有负数无零,把最大的负数找出来,删掉,合并剩余的数

有负数有零,把零和最大的负数合并起来,删掉,合并剩余的数

注意如果只剩下一个数,不能删掉这唯一的一个数

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define ll long long
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
inline int read() {
int p = , w = ; char c = gc();
while(c > '' || c < '') { if(c == '-') w = -; c = gc(); }
while(c >= '' && c <= '') p = p * + c - '', c = gc();
return p * w;
}
int wr[], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = '\n') {
if(!o) pc('');
if(o < ) o = -o, pc('-');
while(o) wr[++ rw] = o % , o /= ;
while(rw) pc(wr[rw --] + '');
pc(c);
}
tpr inline void cmin(ra &a, ra b) { if(a > b) a = b; }
tpr inline void cmax(ra &a, ra b) { if(a < b) a = b; }
tpr inline bool ckmin(ra &a, ra b) { return (a > b) ? a = b, : ; }
tpr inline bool ckmax(ra &a, ra b) { return (a < b) ? a = b, : ; }
}
using namespace std;
using namespace remoon;
#define sid 300050 int n, a[sid]; inline void Solve1() {
rep(i, , n) printf("1 %d %d\n", i, );
} inline void Solve2() { int mip = -1e9 - , pos = ;
rep(i, , n)
if(a[i] < && ckmax(mip, a[i])) pos = i; int fir = -;
rep(i, , n) if(pos != i) { fir = i; break; }
if(fir == -) return; printf("2 %d\n", pos);
if(pos != ) {
rep(i, , n)
if(pos != i) printf("%d %d %d\n", , i, );
}
else rep(i, , n) printf("%d %d %d\n", , i, ); } inline void Solve3() { int lst = -;
rep(i, , n) if(a[i] == ) {
if(lst != -) printf("%d %d %d\n", , lst, i);
lst = i;
} int fir = -;
rep(i, , n) if(a[i] != ) { fir = i; break; }
if(fir == -) return; printf("2 %d\n", lst);
rep(i, fir + , n) if(a[i] != )
printf("%d %d %d\n", , i, fir); } inline void Solve4() { int mip = -1e9 - , pos = ;
rep(i, , n)
if(a[i] < && ckmax(mip, a[i])) pos = i; int lst = -;
rep(i, , n) if(a[i] == || pos == i) {
if(lst != -) printf("%d %d %d\n", , lst, i);
lst = i;
} int fir = -;
rep(i, , n) if(a[i] != && pos != i)
{ fir = i; break; }
if(fir == -) return; printf("2 %d\n", lst);
rep(i, fir + , n) if(a[i] != && pos != i)
printf("%d %d %d\n", , i, fir); } int main() { n = read();
rep(i, , n) a[i] = read(); int neg = , zero = ;
rep(i, , n) if(a[i] < ) neg ^= ;
else if(a[i] == ) zero |= ; if(!zero && !neg) Solve1();
else if(!zero && neg) Solve2();
else if(zero && !neg) Solve3();
else if(zero && neg) Solve4(); return ;
}
05-11 11:17