#include <cstdio> #include <algorithm> #include <utility> #include <vector> using namespace std; typedef pair<int, pair<int, int> > PP; typedef pair<int, pair<int, pair<int, int> > > BAD; int main() { int n, z; scanf("%d%d", &n, &z); vector<PP> good; vector<BAD> bad; for(int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); if(b < a) bad.push_back(make_pair(b - a, make_pair(i, make_pair(a, b)))); else good.push_back(make_pair(a, make_pair(b, i))); } sort(good.begin(), good.end()); sort( bad.begin(), bad.end()); for(int i = 0; i < good.size(); i++) { z -= good[i].first; // printf("g %d %d\n", good[i].first, good[i].second.first); if(z <= 0) { printf("NIE\n"); return 0; } z += good[i].second.first; // printf("left %d\n", z); } for(int i = 0; i < bad.size(); i++) { z -= bad[i].second.second.first; // printf("b %d %d\n", bad[i].first, bad[i].second); if(z <= 0) { printf("NIE\n"); return 0; } z += bad[i].second.second.second; // printf("left %d\n", z); } printf("TAK\n"); for(int i = 0; i < good.size(); i++) { printf("%d ", good[i].second.second + 1); } for(int i = 0; i < bad.size(); i++) { printf("%d ", bad[i].second.first + 1); } printf("\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 | #include <cstdio> #include <algorithm> #include <utility> #include <vector> using namespace std; typedef pair<int, pair<int, int> > PP; typedef pair<int, pair<int, pair<int, int> > > BAD; int main() { int n, z; scanf("%d%d", &n, &z); vector<PP> good; vector<BAD> bad; for(int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); if(b < a) bad.push_back(make_pair(b - a, make_pair(i, make_pair(a, b)))); else good.push_back(make_pair(a, make_pair(b, i))); } sort(good.begin(), good.end()); sort( bad.begin(), bad.end()); for(int i = 0; i < good.size(); i++) { z -= good[i].first; // printf("g %d %d\n", good[i].first, good[i].second.first); if(z <= 0) { printf("NIE\n"); return 0; } z += good[i].second.first; // printf("left %d\n", z); } for(int i = 0; i < bad.size(); i++) { z -= bad[i].second.second.first; // printf("b %d %d\n", bad[i].first, bad[i].second); if(z <= 0) { printf("NIE\n"); return 0; } z += bad[i].second.second.second; // printf("left %d\n", z); } printf("TAK\n"); for(int i = 0; i < good.size(); i++) { printf("%d ", good[i].second.second + 1); } for(int i = 0; i < bad.size(); i++) { printf("%d ", bad[i].second.first + 1); } printf("\n"); return 0; } |