#include<cstdio> #include<algorithm> #include<list> #include<set> using namespace std; const int N = 1E5; const int Q = 1E5; int d[N], a[N], u[N], vp[N], np, v0[N], n0, vd[N], vu[N], nv; bool used[N]; inline int readnum() { int a = 0, c; while((c = getchar() - '0') < 0); for(; c >= 0; c = getchar() - '0') a = 10*a + c; return a; } inline void print(const int* const t, const int n) { for(int i = 0; i < n; i++) { printf("%d ", 1 + t[i]); } } bool comparePositive(const int i, const int j) { return d[i] < d[j] || (d[i] == d[j] && a[i] > a[j]); } bool compareNegativeByD(const int i, const int j) { return d[i] > d[j] || (d[i] == d[j] && a[i] > a[j]); } bool compareNegativeByU(const int i, const int j) { return u[i] > u[j] || (u[i] == u[j] && a[i] > a[j]); } bool brute(const long long z, const set<int> &a, int* const r) { for(set<int>::const_iterator i = a.begin(); i != a.end(); i++) { if (z > d[*i]) { if (a.size() == 1) { *r = *i; return true; } else { set<int> b(a); b.erase(*i); if (brute(z + u[*i], b, r + 1)) { *r = *i; return true; } } } } return false; } inline int nextPos(const int* const t, const int i) { int k = i + 1; while(k < nv && used[t[k]]) k++; return k; } inline void update(const int* const t, long long &z, int &i, int* &r) { if (! used[t[i]]) { *r = t[i]; z += u[t[i]]; r++; used[t[i]] = true; } i = nextPos(t, i); } bool optimized(long long z, int* r) { for(int i = 0, j = 0; (i < nv || j < nv) && z > 0; ) { if (j >= nv) { while(i < nv && z > 0) { update(vu, z, i, r); } } else { int jj = nextPos(vd, j); while(jj < nv && z + u[vd[j]] > d[vd[jj]]) { update(vd, z, j, r); jj = nextPos(vd, j); } } if (i >= nv) { while(j < nv && z > 0) { update(vd, z, j, r); } } else { update(vu, z, i, r); } } return z > 0; } long long sum(long long z, const int* const t, const int n) { for(int i = 0; i < n && z > 0; i++) { z += z > d[t[i]] ? u[t[i]] : -z; } return z; } void brute(long long z) { z = sum(z, vp, np); z = sum(z, v0, n0); int r[nv]; if (z > 0 && (nv == 0 || brute(z, set<int>(vu, vu + nv), r))) { puts("TAK"); print(vp, np); print(v0, n0); print(r, nv); } else { puts("NIE"); } } void optimized(long long z) { z = sum(z, vp, np); z = sum(z, v0, n0); int r[nv]; if (z > 0 && (nv == 0 || optimized(z, r))) { puts("TAK"); print(vp, np); print(v0, n0); print(r, nv); } else { puts("NIE"); } } int main() { const int n = readnum(); const long long z = readnum(); for(int i = 0; i < n; i++) { d[i] = readnum(); a[i] = readnum(); u[i] = a[i] - d[i]; if (u[i] > 0) { vp[np] = i; np++; } else if (u[i] < 0) { vd[nv] = i; vu[nv] = i; nv++; } else { v0[n0] = i; n0++; } } sort(vp, vp + np, comparePositive); sort(vd, vd + nv, compareNegativeByD); sort(vu, vu + nv, compareNegativeByU); optimized(z); }
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #include<cstdio> #include<algorithm> #include<list> #include<set> using namespace std; const int N = 1E5; const int Q = 1E5; int d[N], a[N], u[N], vp[N], np, v0[N], n0, vd[N], vu[N], nv; bool used[N]; inline int readnum() { int a = 0, c; while((c = getchar() - '0') < 0); for(; c >= 0; c = getchar() - '0') a = 10*a + c; return a; } inline void print(const int* const t, const int n) { for(int i = 0; i < n; i++) { printf("%d ", 1 + t[i]); } } bool comparePositive(const int i, const int j) { return d[i] < d[j] || (d[i] == d[j] && a[i] > a[j]); } bool compareNegativeByD(const int i, const int j) { return d[i] > d[j] || (d[i] == d[j] && a[i] > a[j]); } bool compareNegativeByU(const int i, const int j) { return u[i] > u[j] || (u[i] == u[j] && a[i] > a[j]); } bool brute(const long long z, const set<int> &a, int* const r) { for(set<int>::const_iterator i = a.begin(); i != a.end(); i++) { if (z > d[*i]) { if (a.size() == 1) { *r = *i; return true; } else { set<int> b(a); b.erase(*i); if (brute(z + u[*i], b, r + 1)) { *r = *i; return true; } } } } return false; } inline int nextPos(const int* const t, const int i) { int k = i + 1; while(k < nv && used[t[k]]) k++; return k; } inline void update(const int* const t, long long &z, int &i, int* &r) { if (! used[t[i]]) { *r = t[i]; z += u[t[i]]; r++; used[t[i]] = true; } i = nextPos(t, i); } bool optimized(long long z, int* r) { for(int i = 0, j = 0; (i < nv || j < nv) && z > 0; ) { if (j >= nv) { while(i < nv && z > 0) { update(vu, z, i, r); } } else { int jj = nextPos(vd, j); while(jj < nv && z + u[vd[j]] > d[vd[jj]]) { update(vd, z, j, r); jj = nextPos(vd, j); } } if (i >= nv) { while(j < nv && z > 0) { update(vd, z, j, r); } } else { update(vu, z, i, r); } } return z > 0; } long long sum(long long z, const int* const t, const int n) { for(int i = 0; i < n && z > 0; i++) { z += z > d[t[i]] ? u[t[i]] : -z; } return z; } void brute(long long z) { z = sum(z, vp, np); z = sum(z, v0, n0); int r[nv]; if (z > 0 && (nv == 0 || brute(z, set<int>(vu, vu + nv), r))) { puts("TAK"); print(vp, np); print(v0, n0); print(r, nv); } else { puts("NIE"); } } void optimized(long long z) { z = sum(z, vp, np); z = sum(z, v0, n0); int r[nv]; if (z > 0 && (nv == 0 || optimized(z, r))) { puts("TAK"); print(vp, np); print(v0, n0); print(r, nv); } else { puts("NIE"); } } int main() { const int n = readnum(); const long long z = readnum(); for(int i = 0; i < n; i++) { d[i] = readnum(); a[i] = readnum(); u[i] = a[i] - d[i]; if (u[i] > 0) { vp[np] = i; np++; } else if (u[i] < 0) { vd[nv] = i; vu[nv] = i; nv++; } else { v0[n0] = i; n0++; } } sort(vp, vp + np, comparePositive); sort(vd, vd + nv, compareNegativeByD); sort(vu, vu + nv, compareNegativeByU); optimized(z); } |