#include <cstdio> #include <vector> #include <algorithm> #include <map> #include <utility> #define SIZE(x) ((int)(x).size()) #define REP(i,b) for(int i=0; i<(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; const bool DEBUG = false; const int MAXN = 100000; int A[MAXN], B[MAXN]; int E[MAXN]; int n, z; int main() { scanf("%d%d", &n, &z); REP(i,n) scanf("%d%d", &A[i], &B[i]); VI ans; multimap<int,int> D; REP(i,n) { int d = -A[i] + B[i]; E[i] = d; if (d==0) { ans.PB(i); continue; } D.insert(pair<int,int>(d,i)); } VI order; for(multimap<int,int>::reverse_iterator it = D.rbegin(); it != D.rend(); ++it) { if (DEBUG) printf("%d %d\n", it->ST, it->ND); order.PB(it->ND); } int vbr = 0, id = 0; for(auto x: order) { if (E[x]>=0) ans.PB(x); else { vbr = id; if (DEBUG) printf("VBR = %d\n", id); break; } ++id; } if (DEBUG) for(int x: order) printf(" %d\n", x); FORD(i,SIZE(order)-1,vbr) ans.PB(order[i]); for(int x: order) z += E[x]; if (z>0) { printf("TAK\n"); for(auto x: ans) printf("%d ", x+1); } else { printf("NIE\n"); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <utility> #define SIZE(x) ((int)(x).size()) #define REP(i,b) for(int i=0; i<(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; const bool DEBUG = false; const int MAXN = 100000; int A[MAXN], B[MAXN]; int E[MAXN]; int n, z; int main() { scanf("%d%d", &n, &z); REP(i,n) scanf("%d%d", &A[i], &B[i]); VI ans; multimap<int,int> D; REP(i,n) { int d = -A[i] + B[i]; E[i] = d; if (d==0) { ans.PB(i); continue; } D.insert(pair<int,int>(d,i)); } VI order; for(multimap<int,int>::reverse_iterator it = D.rbegin(); it != D.rend(); ++it) { if (DEBUG) printf("%d %d\n", it->ST, it->ND); order.PB(it->ND); } int vbr = 0, id = 0; for(auto x: order) { if (E[x]>=0) ans.PB(x); else { vbr = id; if (DEBUG) printf("VBR = %d\n", id); break; } ++id; } if (DEBUG) for(int x: order) printf(" %d\n", x); FORD(i,SIZE(order)-1,vbr) ans.PB(order[i]); for(int x: order) z += E[x]; if (z>0) { printf("TAK\n"); for(auto x: ans) printf("%d ", x+1); } else { printf("NIE\n"); } return 0; } |