#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int wounds;
queue <int> kolejnosc;
struct monster {
int d;
int a;
int i;
void in(int poz) {
i = poz+1;
cin >> d >> a;
}
void fight() {
wounds += a-d;
kolejnosc.push(i);
}
static bool comp1(monster a, monster b) {
return a.a - a.d > b.a - b.d;
}
static bool comp2(monster a, monster b) {
return a.d > b.d;
}
};
struct comperator {
bool operator() (monster a, monster b) {
if (a.d > b.d) return true;
else if (a.d < b.d) return false;
if (a.a < b.a) return true;
else return false;
}
};
int main() {
int n;
cin >> n;
cin >> wounds;
vector <monster> m(n);
for (vector<monster>::iterator it = m.begin(); it < m.end(); it++) {
it->in(it-m.begin());
}
priority_queue <monster, vector<monster>, comperator> pq;
vector<monster>::iterator it = m.begin();
sort(m.begin(), m.end(), monster::comp1);
for (; it < m.end(); it++) {
while (!pq.empty()) {
if (pq.top().d < wounds) {
monster on_top = pq.top();
pq.pop();
on_top.fight();
} else break;
}
if (it->a - it->d < 0) break;
if (it->d >= wounds) {
pq.push(*it);
} else {
it->fight();
}
}
if (!pq.empty()) {
cout << "NIE";
return 0;
}
sort(it, m.end(), monster::comp2);
for (; it < m.end(); it++) {
if (it->d < wounds) {
it->fight();
} else {
cout << "NIE";
return 0;
}
}
cout << "TAK\n";
while (!kolejnosc.empty()) {
cout << kolejnosc.front() << " ";
kolejnosc.pop();
}
}
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; int wounds; queue <int> kolejnosc; struct monster { int d; int a; int i; void in(int poz) { i = poz+1; cin >> d >> a; } void fight() { wounds += a-d; kolejnosc.push(i); } static bool comp1(monster a, monster b) { return a.a - a.d > b.a - b.d; } static bool comp2(monster a, monster b) { return a.d > b.d; } }; struct comperator { bool operator() (monster a, monster b) { if (a.d > b.d) return true; else if (a.d < b.d) return false; if (a.a < b.a) return true; else return false; } }; int main() { int n; cin >> n; cin >> wounds; vector <monster> m(n); for (vector<monster>::iterator it = m.begin(); it < m.end(); it++) { it->in(it-m.begin()); } priority_queue <monster, vector<monster>, comperator> pq; vector<monster>::iterator it = m.begin(); sort(m.begin(), m.end(), monster::comp1); for (; it < m.end(); it++) { while (!pq.empty()) { if (pq.top().d < wounds) { monster on_top = pq.top(); pq.pop(); on_top.fight(); } else break; } if (it->a - it->d < 0) break; if (it->d >= wounds) { pq.push(*it); } else { it->fight(); } } if (!pq.empty()) { cout << "NIE"; return 0; } sort(it, m.end(), monster::comp2); for (; it < m.end(); it++) { if (it->d < wounds) { it->fight(); } else { cout << "NIE"; return 0; } } cout << "TAK\n"; while (!kolejnosc.empty()) { cout << kolejnosc.front() << " "; kolejnosc.pop(); } } |
English