#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <cctype>
#include <cstdlib>
#define mp make_pair
#define pb push_back
#define _d(fmt, ...) \
do { if (1) fprintf(stderr, fmt, __VA_ARGS__); } while (0)
using namespace std;
struct el {
int p, m;
int s, n;
el(){};
el(int a, int b, int i): m(a), p(b), n(i) {
s = b - a;
}
bool operator < (const el & r) const {
if (s < 0) {
if (r.s >= 0) {
return true;
}
return p < r.p;
} else {
if (r.s < 0) {
return false;
}
return m > r.m;
}
}
};
int main() {
int n, z, a, b, i;
scanf("%d %d", &n, &z);
vector<el> p(n);
vector<el> s;
vector<int> wynik(n);
int zycie = z, ww = 0;
for (i = 0; i < n; ++i) {
scanf("%d %d", &a, &b);
p[i] = el(a, b, i+1);
zycie += b - a;
}
sort(p.begin(), p.end());
bool ok = true, dodatnie = false;
i = 0;
while (i < n || !s.empty()) {
while (i < n && p[i].p < zycie && (p[i].s < 0 || dodatnie)) {
s.pb(p[i]);
i++;
}
if (s.empty()) {
if (p[i].s >= 0) {
if (dodatnie) {
ok = false;
break;
}
dodatnie = true;
continue;
}
ok = false;
break;
}
el x = *s.rbegin();
wynik[ww++] = x.n;
s.pop_back();
zycie += x.m - x.p;
}
if (s.empty() && ok) {
printf("TAK\n");
for (i = n - 1; i >= 0; --i) {
printf("%d ", wynik[i]);
}
} 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 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 | #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <utility> #include <cctype> #include <cstdlib> #define mp make_pair #define pb push_back #define _d(fmt, ...) \ do { if (1) fprintf(stderr, fmt, __VA_ARGS__); } while (0) using namespace std; struct el { int p, m; int s, n; el(){}; el(int a, int b, int i): m(a), p(b), n(i) { s = b - a; } bool operator < (const el & r) const { if (s < 0) { if (r.s >= 0) { return true; } return p < r.p; } else { if (r.s < 0) { return false; } return m > r.m; } } }; int main() { int n, z, a, b, i; scanf("%d %d", &n, &z); vector<el> p(n); vector<el> s; vector<int> wynik(n); int zycie = z, ww = 0; for (i = 0; i < n; ++i) { scanf("%d %d", &a, &b); p[i] = el(a, b, i+1); zycie += b - a; } sort(p.begin(), p.end()); bool ok = true, dodatnie = false; i = 0; while (i < n || !s.empty()) { while (i < n && p[i].p < zycie && (p[i].s < 0 || dodatnie)) { s.pb(p[i]); i++; } if (s.empty()) { if (p[i].s >= 0) { if (dodatnie) { ok = false; break; } dodatnie = true; continue; } ok = false; break; } el x = *s.rbegin(); wynik[ww++] = x.n; s.pop_back(); zycie += x.m - x.p; } if (s.empty() && ok) { printf("TAK\n"); for (i = n - 1; i >= 0; --i) { printf("%d ", wynik[i]); } } else { printf("NIE\n"); } return 0; } |
English