#include <algorithm> #include <cmath> #include <cstdio> #include <list> struct Coupe { int V; int T; bool operator<(const Coupe& k) const { return T<k.T; } }; struct Cup { long double V; int T; Cup(const Coupe& cup) :V(cup.V), T(cup.T) { } }; std::list <Cup> list_from_array(const Coupe* start, const Coupe* end) { std::list <Cup> list; list.push_back(*start); while (++start!=end) { if (start->T==list.back().T) { list.back().V += start->V; } else { list.push_back(*start); } } return list; } int N; Coupe v_have[1000000], v_want[1000000]; unsigned long long sum_products(const Coupe* v) { unsigned long long sum = 0; for (int i = 0; i<N; ++i) { sum += v[i].V*static_cast<unsigned long long>(v[i].T); } return sum; } // true if x < y bool order(long double x, long double y) { return (y-x)>1.0e-11; } // true if x = y bool equal(long double x, long double y) { return fabs(y-x)<=1.0e-11; } class AvailableCups: public std::list<Cup> { public: AvailableCups(std::list <Cup>&& list) :std::list<Cup>(list) { } bool adapt(long double T, std::list<Cup>::iterator& iL) const { if (empty()) { return false; } if (iL==end()) { --iL; } std::list<Cup>::iterator next; while (next = std::next(iL), next!=end() && next->T<=T) { iL = next; } while (iL->T>T) { if (iL==begin()) { return false; } --iL; } return true; } }; bool compute() { if (sum_products(v_have)!=sum_products(v_want)) { return false; } std::sort(v_have, v_have+N); std::sort(v_want, v_want+N); AvailableCups have = list_from_array(v_have, v_have+N); std::list <Cup> want = list_from_array(v_want, v_want+N); std::list<Cup>::iterator iL, iU; // L = lower or equal, U = upper std::list<Cup>::iterator i0 = want.begin(); iL = have.begin(); while (i0!=want.end()) { if (!have.adapt(i0->T, iL)) { return false; } // assert T[iL] <= T[i0] // assert iU = end or T[i0] < T[iU] if (iL->T==i0->T) { // using cup with the same temperature if (order(i0->V, iL->V)) { // have more than needed iL->V -= i0->V; ++i0; } else if (order(iL->V, i0->V)) { // need more than available i0->V -= iL->V; iL = have.erase(iL); } else { // have exactly the same amount i0 = want.erase(i0); iL = have.erase(iL); } } else { // using one cup higher and one cup lower std::list<Cup>::iterator iU = std::next(iL); if (iU==have.end()) { return false; } long double fractionL = static_cast<long double>(iU->T-i0->T)/static_cast<long double>(iU->T-iL->T); long double fractionU = static_cast<long double>(i0->T-iL->T)/static_cast<long double>(iU->T-iL->T); long double VL = i0->V*fractionL; long double VU = i0->V*fractionU; if (order(iL->V, VL) && order(iU->V, VU)) { // both cups are too small long double Vok = std::min(iL->V/fractionL, iU->V/fractionU); VL = Vok*fractionL; VU = Vok*fractionU; i0->V -= VL+VU; } else if (order(iL->V, VL)) { // first cup is too small VL = iL->V; VU = VL*fractionU/fractionL; i0->V -= VL+VU; } else if (order(iU->V, VU)) { // second cup is too small VU = iU->V; VL = VU*fractionL/fractionU; i0->V -= VL+VU; } else { // both cups are fine i0++; } if (order(VU, iU->V)) { iU->V -= VU; } else { iU = have.erase(iU); } if (order(VL, iL->V)) { iL->V -= VL; } else { iL = have.erase(iL); } } } return true; } void run() { if (scanf("%d", &N)!=1) exit(1); for (int i = 0; i<N; ++i) { int V; if (scanf("%d%d%d", &V, &v_have[i].T, &v_want[i].T)!=3) exit(1); v_have[i].V = v_want[i].V = V; } bool result = compute(); puts(result ? "TAK" : "NIE"); } int main() { int Q; if (scanf("%d", &Q)!=1) exit(1); while (Q--) { run(); } }
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 | #include <algorithm> #include <cmath> #include <cstdio> #include <list> struct Coupe { int V; int T; bool operator<(const Coupe& k) const { return T<k.T; } }; struct Cup { long double V; int T; Cup(const Coupe& cup) :V(cup.V), T(cup.T) { } }; std::list <Cup> list_from_array(const Coupe* start, const Coupe* end) { std::list <Cup> list; list.push_back(*start); while (++start!=end) { if (start->T==list.back().T) { list.back().V += start->V; } else { list.push_back(*start); } } return list; } int N; Coupe v_have[1000000], v_want[1000000]; unsigned long long sum_products(const Coupe* v) { unsigned long long sum = 0; for (int i = 0; i<N; ++i) { sum += v[i].V*static_cast<unsigned long long>(v[i].T); } return sum; } // true if x < y bool order(long double x, long double y) { return (y-x)>1.0e-11; } // true if x = y bool equal(long double x, long double y) { return fabs(y-x)<=1.0e-11; } class AvailableCups: public std::list<Cup> { public: AvailableCups(std::list <Cup>&& list) :std::list<Cup>(list) { } bool adapt(long double T, std::list<Cup>::iterator& iL) const { if (empty()) { return false; } if (iL==end()) { --iL; } std::list<Cup>::iterator next; while (next = std::next(iL), next!=end() && next->T<=T) { iL = next; } while (iL->T>T) { if (iL==begin()) { return false; } --iL; } return true; } }; bool compute() { if (sum_products(v_have)!=sum_products(v_want)) { return false; } std::sort(v_have, v_have+N); std::sort(v_want, v_want+N); AvailableCups have = list_from_array(v_have, v_have+N); std::list <Cup> want = list_from_array(v_want, v_want+N); std::list<Cup>::iterator iL, iU; // L = lower or equal, U = upper std::list<Cup>::iterator i0 = want.begin(); iL = have.begin(); while (i0!=want.end()) { if (!have.adapt(i0->T, iL)) { return false; } // assert T[iL] <= T[i0] // assert iU = end or T[i0] < T[iU] if (iL->T==i0->T) { // using cup with the same temperature if (order(i0->V, iL->V)) { // have more than needed iL->V -= i0->V; ++i0; } else if (order(iL->V, i0->V)) { // need more than available i0->V -= iL->V; iL = have.erase(iL); } else { // have exactly the same amount i0 = want.erase(i0); iL = have.erase(iL); } } else { // using one cup higher and one cup lower std::list<Cup>::iterator iU = std::next(iL); if (iU==have.end()) { return false; } long double fractionL = static_cast<long double>(iU->T-i0->T)/static_cast<long double>(iU->T-iL->T); long double fractionU = static_cast<long double>(i0->T-iL->T)/static_cast<long double>(iU->T-iL->T); long double VL = i0->V*fractionL; long double VU = i0->V*fractionU; if (order(iL->V, VL) && order(iU->V, VU)) { // both cups are too small long double Vok = std::min(iL->V/fractionL, iU->V/fractionU); VL = Vok*fractionL; VU = Vok*fractionU; i0->V -= VL+VU; } else if (order(iL->V, VL)) { // first cup is too small VL = iL->V; VU = VL*fractionU/fractionL; i0->V -= VL+VU; } else if (order(iU->V, VU)) { // second cup is too small VU = iU->V; VL = VU*fractionL/fractionU; i0->V -= VL+VU; } else { // both cups are fine i0++; } if (order(VU, iU->V)) { iU->V -= VU; } else { iU = have.erase(iU); } if (order(VL, iL->V)) { iL->V -= VL; } else { iL = have.erase(iL); } } } return true; } void run() { if (scanf("%d", &N)!=1) exit(1); for (int i = 0; i<N; ++i) { int V; if (scanf("%d%d%d", &V, &v_have[i].T, &v_want[i].T)!=3) exit(1); v_have[i].V = v_want[i].V = V; } bool result = compute(); puts(result ? "TAK" : "NIE"); } int main() { int Q; if (scanf("%d", &Q)!=1) exit(1); while (Q--) { run(); } } |