#include <iostream> #include <vector> using namespace std; vector<int> *pd, *pa, *pi, *md, *ma, *mi; void wstawPlus(int d, int a, int index) { for(int i=0; i<pd->size(); i++) { if(pd->at(i) > d) { pd->insert(pd->begin()+i,d); pa->insert(pa->begin()+i,a); pi->insert(pi->begin()+i,index); return; } } pd->push_back(d); pa->push_back(a); pi->push_back(index); return; } void wstawMinus(int d, int a, int index) { for(int i=0; i<md->size(); i++) { if(md->at(i) == d) { if(ma->at(i) < a) { md->insert(md->begin()+i,d); ma->insert(ma->begin()+i,a); mi->insert(mi->begin()+i,index); return; } } if(md->at(i) < d) { md->insert(md->begin()+i,d); ma->insert(ma->begin()+i,a); mi->insert(mi->begin()+i,index); return; } } md->push_back(d); ma->push_back(a); mi->push_back(index); return; } int main() { int n = 0, z = 0; cin >> n >> z; int d = 0, a = 0, r = 0; vector<int> *w = new vector<int>(); int s = 0; pd = new vector<int>(); pa = new vector<int>(); pi = new vector<int>(); md = new vector<int>(); ma = new vector<int>(); mi = new vector<int>(); for(int i=0; i<n; i++) { cin >> d >> a; r = a - d; s += r; if(r >= 0) { wstawPlus(d,a,i+1); } else { wstawMinus(d,a,i+1); } } for(int i=0; i<pd->size(); i++) { z -= pd->at(i); if(z <= 0) { cout << "NIE" << endl; return 0; } else { w->push_back(pi->at(i)); z += pa->at(i); } } for(int i=0; i<md->size(); i++) { z -= md->at(i); if(z <= 0) { cout << "NIE" << endl; return 0; } else { w->push_back(mi->at(i)); z += ma->at(i); } } cout << "TAK" << endl; for(int i=0; i<w->size(); i++) cout << w->at(i) << " "; cout << endl; 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <vector> using namespace std; vector<int> *pd, *pa, *pi, *md, *ma, *mi; void wstawPlus(int d, int a, int index) { for(int i=0; i<pd->size(); i++) { if(pd->at(i) > d) { pd->insert(pd->begin()+i,d); pa->insert(pa->begin()+i,a); pi->insert(pi->begin()+i,index); return; } } pd->push_back(d); pa->push_back(a); pi->push_back(index); return; } void wstawMinus(int d, int a, int index) { for(int i=0; i<md->size(); i++) { if(md->at(i) == d) { if(ma->at(i) < a) { md->insert(md->begin()+i,d); ma->insert(ma->begin()+i,a); mi->insert(mi->begin()+i,index); return; } } if(md->at(i) < d) { md->insert(md->begin()+i,d); ma->insert(ma->begin()+i,a); mi->insert(mi->begin()+i,index); return; } } md->push_back(d); ma->push_back(a); mi->push_back(index); return; } int main() { int n = 0, z = 0; cin >> n >> z; int d = 0, a = 0, r = 0; vector<int> *w = new vector<int>(); int s = 0; pd = new vector<int>(); pa = new vector<int>(); pi = new vector<int>(); md = new vector<int>(); ma = new vector<int>(); mi = new vector<int>(); for(int i=0; i<n; i++) { cin >> d >> a; r = a - d; s += r; if(r >= 0) { wstawPlus(d,a,i+1); } else { wstawMinus(d,a,i+1); } } for(int i=0; i<pd->size(); i++) { z -= pd->at(i); if(z <= 0) { cout << "NIE" << endl; return 0; } else { w->push_back(pi->at(i)); z += pa->at(i); } } for(int i=0; i<md->size(); i++) { z -= md->at(i); if(z <= 0) { cout << "NIE" << endl; return 0; } else { w->push_back(mi->at(i)); z += ma->at(i); } } cout << "TAK" << endl; for(int i=0; i<w->size(); i++) cout << w->at(i) << " "; cout << endl; return 0; } |