#include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr #else #define LOG if (false) cerr #endif typedef long long ll; #ifdef USE_FILE_CIN ifstream input("../her.in"); #define cin input #endif struct Mug { int idx; int liters; int initTemp; int destTemp; }; struct Stats { ll initEnergy; ll destEnergy; int minInitTemp; int maxInitTemp; int minDestTemp; int maxDestTemp; }; vector<Mug> mugs; int n; inline bool compByInitTemp(const Mug& mug1, const Mug& mug2) { return mug1.initTemp < mug2.initTemp; } inline bool compByDestTemp(const Mug& mug1, const Mug& mug2) { return mug1.destTemp < mug2.destTemp; } void printMugs(const vector<Mug>& mugs) { #ifdef USE_CERR_LOG for (const Mug& mug : mugs) { LOG << mug.idx << ": " << mug.liters << ", " << mug.initTemp << ", " << mug.destTemp << endl; } LOG << endl; #endif } Stats computeStats() { Stats stats {0, 0, 9'999'999, 0, 9'999'999, 0}; for (Mug& mug : mugs) { stats.initEnergy += (ll) mug.liters * mug.initTemp; stats.destEnergy += (ll) mug.liters * mug.destTemp; stats.minInitTemp = min(stats.minInitTemp, mug.initTemp); stats.maxInitTemp = max(stats.maxInitTemp, mug.initTemp); stats.minDestTemp = min(stats.minDestTemp, mug.destTemp); stats.maxDestTemp = max(stats.maxDestTemp, mug.destTemp); } return stats; } void printStats(const Stats& stats) { LOG << stats.initEnergy << ", " << stats.destEnergy << endl; } void readData() { int i = 0; cin >> n; mugs.resize(n); for (Mug& mug : mugs) { cin >> mug.liters >> mug.initTemp >> mug.destTemp; mug.idx = i++; } } bool isInitLowerThatDest(const vector<Mug>& mugs) { vector<Mug> byInitTemp = mugs; vector<Mug> byDestTemp = mugs; sort(byInitTemp.begin(), byInitTemp.end(), compByInitTemp); sort(byDestTemp.begin(), byDestTemp.end(), compByDestTemp); printMugs(byInitTemp); LOG << endl << endl; printMugs(byDestTemp); size_t initIdx = 0; size_t destIdx = 0; size_t mugsSize = mugs.size(); ll initEnergy = 0; ll destEnergy = 0; ll initLiters = byInitTemp[0].liters; ll destLiters = byDestTemp[0].liters; while (initIdx < mugsSize && destIdx < mugsSize) { ll litersToTake = min(initLiters, destLiters); initEnergy += litersToTake * byInitTemp[initIdx].initTemp; destEnergy += litersToTake * byDestTemp[destIdx].destTemp; if (initEnergy > destEnergy) { return false; } initLiters -= litersToTake; destLiters -= litersToTake; if (initLiters == 0) { initIdx++; if (initIdx < mugs.size()) { initLiters = byInitTemp[initIdx].liters; } } if (destLiters == 0) { destIdx++; if (destIdx < mugsSize) { destLiters = byDestTemp[destIdx].liters; } } } return true; } bool solve() { Stats stats = computeStats(); printStats(stats); if ((stats.initEnergy != stats.destEnergy) || (stats.minInitTemp > stats.minDestTemp) || (stats.maxInitTemp < stats.maxDestTemp)) { return false; } return isInitLowerThatDest(mugs); } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { readData(); cout << (solve() ? "TAK\n" : "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 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 | #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr #else #define LOG if (false) cerr #endif typedef long long ll; #ifdef USE_FILE_CIN ifstream input("../her.in"); #define cin input #endif struct Mug { int idx; int liters; int initTemp; int destTemp; }; struct Stats { ll initEnergy; ll destEnergy; int minInitTemp; int maxInitTemp; int minDestTemp; int maxDestTemp; }; vector<Mug> mugs; int n; inline bool compByInitTemp(const Mug& mug1, const Mug& mug2) { return mug1.initTemp < mug2.initTemp; } inline bool compByDestTemp(const Mug& mug1, const Mug& mug2) { return mug1.destTemp < mug2.destTemp; } void printMugs(const vector<Mug>& mugs) { #ifdef USE_CERR_LOG for (const Mug& mug : mugs) { LOG << mug.idx << ": " << mug.liters << ", " << mug.initTemp << ", " << mug.destTemp << endl; } LOG << endl; #endif } Stats computeStats() { Stats stats {0, 0, 9'999'999, 0, 9'999'999, 0}; for (Mug& mug : mugs) { stats.initEnergy += (ll) mug.liters * mug.initTemp; stats.destEnergy += (ll) mug.liters * mug.destTemp; stats.minInitTemp = min(stats.minInitTemp, mug.initTemp); stats.maxInitTemp = max(stats.maxInitTemp, mug.initTemp); stats.minDestTemp = min(stats.minDestTemp, mug.destTemp); stats.maxDestTemp = max(stats.maxDestTemp, mug.destTemp); } return stats; } void printStats(const Stats& stats) { LOG << stats.initEnergy << ", " << stats.destEnergy << endl; } void readData() { int i = 0; cin >> n; mugs.resize(n); for (Mug& mug : mugs) { cin >> mug.liters >> mug.initTemp >> mug.destTemp; mug.idx = i++; } } bool isInitLowerThatDest(const vector<Mug>& mugs) { vector<Mug> byInitTemp = mugs; vector<Mug> byDestTemp = mugs; sort(byInitTemp.begin(), byInitTemp.end(), compByInitTemp); sort(byDestTemp.begin(), byDestTemp.end(), compByDestTemp); printMugs(byInitTemp); LOG << endl << endl; printMugs(byDestTemp); size_t initIdx = 0; size_t destIdx = 0; size_t mugsSize = mugs.size(); ll initEnergy = 0; ll destEnergy = 0; ll initLiters = byInitTemp[0].liters; ll destLiters = byDestTemp[0].liters; while (initIdx < mugsSize && destIdx < mugsSize) { ll litersToTake = min(initLiters, destLiters); initEnergy += litersToTake * byInitTemp[initIdx].initTemp; destEnergy += litersToTake * byDestTemp[destIdx].destTemp; if (initEnergy > destEnergy) { return false; } initLiters -= litersToTake; destLiters -= litersToTake; if (initLiters == 0) { initIdx++; if (initIdx < mugs.size()) { initLiters = byInitTemp[initIdx].liters; } } if (destLiters == 0) { destIdx++; if (destIdx < mugsSize) { destLiters = byDestTemp[destIdx].liters; } } } return true; } bool solve() { Stats stats = computeStats(); printStats(stats); if ((stats.initEnergy != stats.destEnergy) || (stats.minInitTemp > stats.minDestTemp) || (stats.maxInitTemp < stats.maxDestTemp)) { return false; } return isInitLowerThatDest(mugs); } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { readData(); cout << (solve() ? "TAK\n" : "NIE\n"); } return 0; } |