#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#include <functional>
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;
struct M {
int nr,str,gain;
M() { }
M(int nr, int str, int gain) : nr(nr), str(str), gain(gain) { }
};
struct Problem {
int n;
vector<M> ms;
Problem(int n) : n(n), ms(n) { }
VI Go(LL z);
};
struct ByGainDec {
bool operator()(const M& m1, const M& m2) const
{ if (m1.gain != m2.gain) return m1.gain > m2.gain; return m1.nr < m2.nr; }
};
struct ByStr {
bool operator()(const M& m1, const M& m2) const
{ if (m1.str != m2.str) return m1.str < m2.str; return m1.nr < m2.nr; }
};
struct ByRecDec {
bool operator()(const M& m1, const M& m2) const
{ if (m1.str+m1.gain != m2.str+m2.gain) return m1.str+m1.gain > m2.str+m2.gain; return m1.nr < m2.nr; }
};
VI Problem::Go(LL z)
{
VI res;
sort(ALL(ms), ByGainDec());
int pos = 0;
while (pos < n && ms[pos].gain >= 0) ++pos;
sort(ms.begin(), ms.begin() + pos, ByStr());
REP(i,pos) {
if (z > ms[i].str) {
z += ms[i].gain;
res.push_back(ms[i].nr);
}
else return VI();
}
if (pos == n) return res;
sort(ms.begin() + pos, ms.end(), ByRecDec());
FOR(i,pos,n-1) {
if (z > ms[i].str) {
z += ms[i].gain;
res.push_back(ms[i].nr);
}
else return VI();
}
return res;
}
int main(int argc, char** argv)
{
int n,z;
scanf("%d%d", &n, &z);
Problem p(n);
REP(i,n) {
int d,a;
scanf("%d%d", &d, &a);
p.ms[i].str = d;
p.ms[i].gain = a-d;
p.ms[i].nr = i;
}
VI r = p.Go(z);
if (r.empty()) printf("NIE\n");
else {
printf("TAK\n");
FOREACH(it,r) printf("%d ", *it + 1);
printf("\n");
}
#ifdef _DEBUG
system("pause");
#endif
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 | #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #include <functional> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; const int INF = 1000000001; struct M { int nr,str,gain; M() { } M(int nr, int str, int gain) : nr(nr), str(str), gain(gain) { } }; struct Problem { int n; vector<M> ms; Problem(int n) : n(n), ms(n) { } VI Go(LL z); }; struct ByGainDec { bool operator()(const M& m1, const M& m2) const { if (m1.gain != m2.gain) return m1.gain > m2.gain; return m1.nr < m2.nr; } }; struct ByStr { bool operator()(const M& m1, const M& m2) const { if (m1.str != m2.str) return m1.str < m2.str; return m1.nr < m2.nr; } }; struct ByRecDec { bool operator()(const M& m1, const M& m2) const { if (m1.str+m1.gain != m2.str+m2.gain) return m1.str+m1.gain > m2.str+m2.gain; return m1.nr < m2.nr; } }; VI Problem::Go(LL z) { VI res; sort(ALL(ms), ByGainDec()); int pos = 0; while (pos < n && ms[pos].gain >= 0) ++pos; sort(ms.begin(), ms.begin() + pos, ByStr()); REP(i,pos) { if (z > ms[i].str) { z += ms[i].gain; res.push_back(ms[i].nr); } else return VI(); } if (pos == n) return res; sort(ms.begin() + pos, ms.end(), ByRecDec()); FOR(i,pos,n-1) { if (z > ms[i].str) { z += ms[i].gain; res.push_back(ms[i].nr); } else return VI(); } return res; } int main(int argc, char** argv) { int n,z; scanf("%d%d", &n, &z); Problem p(n); REP(i,n) { int d,a; scanf("%d%d", &d, &a); p.ms[i].str = d; p.ms[i].gain = a-d; p.ms[i].nr = i; } VI r = p.Go(z); if (r.empty()) printf("NIE\n"); else { printf("TAK\n"); FOREACH(it,r) printf("%d ", *it + 1); printf("\n"); } #ifdef _DEBUG system("pause"); #endif return 0; } |
English