#include <bits/stdc++.h> typedef long long int ll; using namespace std; struct Fraction { ll num, den; Fraction() { } Fraction(ll _num, ll _den) { num = _num; den = _den; } friend ostream& operator <<(ostream& s, const Fraction& fraction) { return s << fraction.num << "/" << fraction.den; } bool const operator <(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum < newOtherNum; } bool const operator >(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum > newOtherNum; } bool const operator >=(const Fraction& other) const { return *this > other || *this == other; } bool const operator ==(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum == newOtherNum; } bool const operator !=(const Fraction& other) const { return !(*this == other); } Fraction& operator +=(const Fraction& other) { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); num = newNum + newOtherNum; den = denLcm; ll resGcd = gcd(num, den); num /= resGcd; den /= resGcd; return *this; } Fraction& operator -=(const Fraction& other) { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); num = newNum - newOtherNum; den = denLcm; ll resGcd = gcd(num, den); num /= resGcd; den /= resGcd; return *this; } friend Fraction operator -(Fraction first, const Fraction& second) { ll denLcm = lcm(first.den, second.den); ll newFirstNum = first.num * (denLcm / first.den); ll newSecondNum = second.num * (denLcm / second.den); Fraction res(newFirstNum - newSecondNum, denLcm); ll resGcd = gcd(res.num, res.den); res.num /= resGcd; res.den /= resGcd; return res; } friend Fraction operator +(Fraction first, const Fraction& second) { ll denLcm = lcm(first.den, second.den); ll newFirstNum = first.num * (denLcm / first.den); ll newSecondNum = second.num * (denLcm / second.den); Fraction res(newFirstNum + newSecondNum, denLcm); ll resGcd = gcd(res.num, res.den); res.num /= resGcd; res.den /= resGcd; return res; } friend Fraction operator *(Fraction first, const Fraction& second) { Fraction res(first.num, first.den); ll div = gcd(first.num, first.den); res.num /= div; res.den /= div; div = gcd(res.num, second.den); res.num /= div; res.den *= (second.den / div); div = gcd(res.den, second.num); res.num *= (second.num / div); res.den /= div; div = gcd(res.num, res.den); res.num /= div; res.den /= div; return res; } friend Fraction operator /(Fraction first, const Fraction& second) { Fraction secondInverted(second.den, second.num); return first * secondInverted; } bool isNegative() { if (num == 0) return false; if (num < 0) return den > 0; else if (num > 0) return den < 0; } bool isDenZero() { return den == 0; } void reduce() { ll div = gcd(num, den); num /= div; den /= div; } }; int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; int minAvailableTemperature = numeric_limits<int>::max(), maxAvailableTemperature = numeric_limits<int>::min(), minExpectedTemperature = numeric_limits<int>::max(), maxExpectedTemperature = numeric_limits<int>::min(); // temperature, amount // shall we go for fractions? maybe doubles are better? // fractions seem more accurate, but not quite sure if we won't encounter overflows... map<Fraction, Fraction> expected, curr; for (int i = 0; i < n; ++i) { int amount, currTemperature, expectedTemperature; cin >> amount >> currTemperature >> expectedTemperature; minAvailableTemperature = min(minAvailableTemperature, currTemperature); maxAvailableTemperature = max(maxAvailableTemperature, currTemperature); minExpectedTemperature = min(minExpectedTemperature, expectedTemperature); maxExpectedTemperature = max(maxExpectedTemperature, expectedTemperature); auto ret = expected.insert(make_pair(Fraction((ll) expectedTemperature, 1LL), Fraction((ll) amount, 1LL))); if (!ret.second) ret.first->second += Fraction((ll) amount, 1LL); ret = curr.insert(make_pair(Fraction((ll) currTemperature, 1LL), Fraction((ll) amount, 1LL))); if (!ret.second) ret.first->second += Fraction((ll) amount, 1LL); } if (minAvailableTemperature > minExpectedTemperature || maxAvailableTemperature < maxExpectedTemperature) { cout << "NIE\n"; continue; } bool ok = true; for (auto& exp : expected) { pair<Fraction, Fraction> now = *curr.begin(); curr.erase(curr.begin()); // lowest available temperature is beyond the expected value; we can't lower it. if (now.first > exp.first) { ok = false; break; } if (now.first == exp.first) // temperature is as expected. { if (now.second == exp.second) continue; else if (now.second > exp.second) { // pour the excess into a new mug. curr.insert(make_pair(now.first, now.second - exp.second)); continue; } else // temperature is right, but we don't have enough tea. mixing would lead to a higher temperature, hence impossible. { ok = false; break; } } while (now.first != exp.first || now.second < exp.second) { if (curr.size() > 0) { auto next = *curr.begin(); // amount that we take from next mug; we mix it with our current mixture to get expected temperature. auto amountFromNext = (now.second * (exp.first - now.first)) / (next.first - exp.first); if (amountFromNext.isNegative() || amountFromNext.isDenZero() || amountFromNext > next.second) { // take all from next mug // weighted average now.first = (now.first * now.second + next.first * next.second) / (now.second + next.second); now.second += next.second; now.first.reduce(); curr.erase(curr.begin()); } else // we can reach expected temperature by mixing with some amount from next mug { now.first = (now.first * now.second + next.first * amountFromNext) / (now.second + amountFromNext); now.second += amountFromNext; now.first.reduce(); // assert(now.first == exp.first); // remove tea from next mug auto it = curr.begin(); it->second -= amountFromNext; // temperature is as expected, what about amount of tea? if (now.second < exp.second) { ok = false; break; } else if (now.second > exp.second) { // remove the excess, and pour into a new mug. curr.insert(make_pair(exp.first, now.second - exp.second)); } // assert(now.first == exp.first && now.second >= exp.second); } } else { ok = false; break; } } if (!ok) break; } if (!ok) cout << "NIE\n"; else cout << "TAK\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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | #include <bits/stdc++.h> typedef long long int ll; using namespace std; struct Fraction { ll num, den; Fraction() { } Fraction(ll _num, ll _den) { num = _num; den = _den; } friend ostream& operator <<(ostream& s, const Fraction& fraction) { return s << fraction.num << "/" << fraction.den; } bool const operator <(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum < newOtherNum; } bool const operator >(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum > newOtherNum; } bool const operator >=(const Fraction& other) const { return *this > other || *this == other; } bool const operator ==(const Fraction& other) const { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); return newNum == newOtherNum; } bool const operator !=(const Fraction& other) const { return !(*this == other); } Fraction& operator +=(const Fraction& other) { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); num = newNum + newOtherNum; den = denLcm; ll resGcd = gcd(num, den); num /= resGcd; den /= resGcd; return *this; } Fraction& operator -=(const Fraction& other) { ll denLcm = lcm(den, other.den); ll newNum = num * (denLcm / den); ll newOtherNum = other.num * (denLcm / other.den); num = newNum - newOtherNum; den = denLcm; ll resGcd = gcd(num, den); num /= resGcd; den /= resGcd; return *this; } friend Fraction operator -(Fraction first, const Fraction& second) { ll denLcm = lcm(first.den, second.den); ll newFirstNum = first.num * (denLcm / first.den); ll newSecondNum = second.num * (denLcm / second.den); Fraction res(newFirstNum - newSecondNum, denLcm); ll resGcd = gcd(res.num, res.den); res.num /= resGcd; res.den /= resGcd; return res; } friend Fraction operator +(Fraction first, const Fraction& second) { ll denLcm = lcm(first.den, second.den); ll newFirstNum = first.num * (denLcm / first.den); ll newSecondNum = second.num * (denLcm / second.den); Fraction res(newFirstNum + newSecondNum, denLcm); ll resGcd = gcd(res.num, res.den); res.num /= resGcd; res.den /= resGcd; return res; } friend Fraction operator *(Fraction first, const Fraction& second) { Fraction res(first.num, first.den); ll div = gcd(first.num, first.den); res.num /= div; res.den /= div; div = gcd(res.num, second.den); res.num /= div; res.den *= (second.den / div); div = gcd(res.den, second.num); res.num *= (second.num / div); res.den /= div; div = gcd(res.num, res.den); res.num /= div; res.den /= div; return res; } friend Fraction operator /(Fraction first, const Fraction& second) { Fraction secondInverted(second.den, second.num); return first * secondInverted; } bool isNegative() { if (num == 0) return false; if (num < 0) return den > 0; else if (num > 0) return den < 0; } bool isDenZero() { return den == 0; } void reduce() { ll div = gcd(num, den); num /= div; den /= div; } }; int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; int minAvailableTemperature = numeric_limits<int>::max(), maxAvailableTemperature = numeric_limits<int>::min(), minExpectedTemperature = numeric_limits<int>::max(), maxExpectedTemperature = numeric_limits<int>::min(); // temperature, amount // shall we go for fractions? maybe doubles are better? // fractions seem more accurate, but not quite sure if we won't encounter overflows... map<Fraction, Fraction> expected, curr; for (int i = 0; i < n; ++i) { int amount, currTemperature, expectedTemperature; cin >> amount >> currTemperature >> expectedTemperature; minAvailableTemperature = min(minAvailableTemperature, currTemperature); maxAvailableTemperature = max(maxAvailableTemperature, currTemperature); minExpectedTemperature = min(minExpectedTemperature, expectedTemperature); maxExpectedTemperature = max(maxExpectedTemperature, expectedTemperature); auto ret = expected.insert(make_pair(Fraction((ll) expectedTemperature, 1LL), Fraction((ll) amount, 1LL))); if (!ret.second) ret.first->second += Fraction((ll) amount, 1LL); ret = curr.insert(make_pair(Fraction((ll) currTemperature, 1LL), Fraction((ll) amount, 1LL))); if (!ret.second) ret.first->second += Fraction((ll) amount, 1LL); } if (minAvailableTemperature > minExpectedTemperature || maxAvailableTemperature < maxExpectedTemperature) { cout << "NIE\n"; continue; } bool ok = true; for (auto& exp : expected) { pair<Fraction, Fraction> now = *curr.begin(); curr.erase(curr.begin()); // lowest available temperature is beyond the expected value; we can't lower it. if (now.first > exp.first) { ok = false; break; } if (now.first == exp.first) // temperature is as expected. { if (now.second == exp.second) continue; else if (now.second > exp.second) { // pour the excess into a new mug. curr.insert(make_pair(now.first, now.second - exp.second)); continue; } else // temperature is right, but we don't have enough tea. mixing would lead to a higher temperature, hence impossible. { ok = false; break; } } while (now.first != exp.first || now.second < exp.second) { if (curr.size() > 0) { auto next = *curr.begin(); // amount that we take from next mug; we mix it with our current mixture to get expected temperature. auto amountFromNext = (now.second * (exp.first - now.first)) / (next.first - exp.first); if (amountFromNext.isNegative() || amountFromNext.isDenZero() || amountFromNext > next.second) { // take all from next mug // weighted average now.first = (now.first * now.second + next.first * next.second) / (now.second + next.second); now.second += next.second; now.first.reduce(); curr.erase(curr.begin()); } else // we can reach expected temperature by mixing with some amount from next mug { now.first = (now.first * now.second + next.first * amountFromNext) / (now.second + amountFromNext); now.second += amountFromNext; now.first.reduce(); // assert(now.first == exp.first); // remove tea from next mug auto it = curr.begin(); it->second -= amountFromNext; // temperature is as expected, what about amount of tea? if (now.second < exp.second) { ok = false; break; } else if (now.second > exp.second) { // remove the excess, and pour into a new mug. curr.insert(make_pair(exp.first, now.second - exp.second)); } // assert(now.first == exp.first && now.second >= exp.second); } } else { ok = false; break; } } if (!ok) break; } if (!ok) cout << "NIE\n"; else cout << "TAK\n"; } return 0; } |