#include <iostream> #include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main() { int t; scanf("%d", &t); for (int o = 0; o < t; o++) { int n, amount, t1, t2; scanf("%d", &n); vector<pair<int, int> > heatT1, coldT1, heatT2, coldT2; long long int saldo = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &amount, &t1, &t2); if (t1 < t2) { heatT1.push_back(make_pair(t1, amount)); heatT2.push_back(make_pair(t2, amount)); heatT2.push_back(make_pair(t1, -amount)); } else if (t1 > t2) { coldT1.push_back(make_pair(t1, amount)); coldT2.push_back(make_pair(t2, amount)); coldT2.push_back(make_pair(t1, -amount)); } saldo = saldo + (t1 - t2) * amount; } if (saldo != 0) { printf("NIE\n"); continue; } //saldo == 0 sort(heatT1.begin(), heatT1.end()); sort(heatT2.begin(), heatT2.end()); sort(coldT1.begin(), coldT1.end()); sort(coldT2.begin(), coldT2.end()); reverse(heatT2.begin(), heatT2.end()); reverse(coldT1.begin(), coldT1.end()); bool isOK = true; long long int potential = 0; long long int oppositePotential = 0; long long int counter = 0; int heatIndx = 0, coldIndx = 0; //printf("heatT2: "); //for (int i=0; i< heatT2.size(); i++) // printf("%d ",heatT2[i].first); //printf("coldT1: "); //for (int i=0; i< coldT1.size(); i++) // printf("%d ",coldT1[i].first); int last = -1; while (heatIndx < heatT2.size() && coldIndx < coldT1.size()) { if (heatIndx == heatT2.size() || (coldIndx < coldT1.size() && heatT2[heatIndx].first <= coldT1[coldIndx].first)) { if (last > -1) counter += (potential - oppositePotential) * (last - coldT1[coldIndx].first); last = coldT1[coldIndx].first; counter += coldT1[coldIndx].second; potential += coldT1[coldIndx].second; coldIndx++; } else { if (last > -1) counter += (potential - oppositePotential) * (last - heatT2[heatIndx].first); last = heatT2[heatIndx].first; if (heatT2[heatIndx].second > 0) counter -= heatT2[heatIndx].second; oppositePotential += heatT2[heatIndx].second; heatIndx++; } //printf("counter: %lld, potential: %lld, oppositePotential: %lld\n", counter, potential, oppositePotential); if (counter < 0) { isOK = false; break; } } if (!isOK) { printf("NIE\n"); continue; } counter = 0; potential = 0; oppositePotential = 0; heatIndx = 0, coldIndx = 0; //printf("heatT1: "); //for (int i=0; i< heatT1.size(); i++) // printf("%d ",heatT1[i].first); //printf("coldT2: "); //for (int i=0; i< coldT2.size(); i++) // printf("%d ",coldT2[i].first); last = -1; while (heatIndx < heatT1.size() && coldIndx < coldT2.size()) { if (coldIndx == coldT2.size() || (heatIndx < heatT1.size() && heatT1[heatIndx].first <= coldT2[coldIndx].first)) { if (last > -1) counter += (potential - oppositePotential) * (heatT1[heatIndx].first - last); last = heatT1[heatIndx].first; counter += heatT1[heatIndx].second; potential += heatT1[heatIndx].second; heatIndx++; } else { if (last > -1) counter += (potential - oppositePotential) * (coldT2[coldIndx].first - last); last = coldT2[coldIndx].first; if (coldT2[coldIndx].second > 0) counter -= coldT2[coldIndx].second; oppositePotential += coldT2[coldIndx].second; coldIndx++; } //printf("counter: %d, potential: %d\n",counter, potential); if (counter < 0) { isOK = false; break; } } if (!isOK) printf("NIE\n"); else printf("TAK\n"); } }
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 | #include <iostream> #include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main() { int t; scanf("%d", &t); for (int o = 0; o < t; o++) { int n, amount, t1, t2; scanf("%d", &n); vector<pair<int, int> > heatT1, coldT1, heatT2, coldT2; long long int saldo = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &amount, &t1, &t2); if (t1 < t2) { heatT1.push_back(make_pair(t1, amount)); heatT2.push_back(make_pair(t2, amount)); heatT2.push_back(make_pair(t1, -amount)); } else if (t1 > t2) { coldT1.push_back(make_pair(t1, amount)); coldT2.push_back(make_pair(t2, amount)); coldT2.push_back(make_pair(t1, -amount)); } saldo = saldo + (t1 - t2) * amount; } if (saldo != 0) { printf("NIE\n"); continue; } //saldo == 0 sort(heatT1.begin(), heatT1.end()); sort(heatT2.begin(), heatT2.end()); sort(coldT1.begin(), coldT1.end()); sort(coldT2.begin(), coldT2.end()); reverse(heatT2.begin(), heatT2.end()); reverse(coldT1.begin(), coldT1.end()); bool isOK = true; long long int potential = 0; long long int oppositePotential = 0; long long int counter = 0; int heatIndx = 0, coldIndx = 0; //printf("heatT2: "); //for (int i=0; i< heatT2.size(); i++) // printf("%d ",heatT2[i].first); //printf("coldT1: "); //for (int i=0; i< coldT1.size(); i++) // printf("%d ",coldT1[i].first); int last = -1; while (heatIndx < heatT2.size() && coldIndx < coldT1.size()) { if (heatIndx == heatT2.size() || (coldIndx < coldT1.size() && heatT2[heatIndx].first <= coldT1[coldIndx].first)) { if (last > -1) counter += (potential - oppositePotential) * (last - coldT1[coldIndx].first); last = coldT1[coldIndx].first; counter += coldT1[coldIndx].second; potential += coldT1[coldIndx].second; coldIndx++; } else { if (last > -1) counter += (potential - oppositePotential) * (last - heatT2[heatIndx].first); last = heatT2[heatIndx].first; if (heatT2[heatIndx].second > 0) counter -= heatT2[heatIndx].second; oppositePotential += heatT2[heatIndx].second; heatIndx++; } //printf("counter: %lld, potential: %lld, oppositePotential: %lld\n", counter, potential, oppositePotential); if (counter < 0) { isOK = false; break; } } if (!isOK) { printf("NIE\n"); continue; } counter = 0; potential = 0; oppositePotential = 0; heatIndx = 0, coldIndx = 0; //printf("heatT1: "); //for (int i=0; i< heatT1.size(); i++) // printf("%d ",heatT1[i].first); //printf("coldT2: "); //for (int i=0; i< coldT2.size(); i++) // printf("%d ",coldT2[i].first); last = -1; while (heatIndx < heatT1.size() && coldIndx < coldT2.size()) { if (coldIndx == coldT2.size() || (heatIndx < heatT1.size() && heatT1[heatIndx].first <= coldT2[coldIndx].first)) { if (last > -1) counter += (potential - oppositePotential) * (heatT1[heatIndx].first - last); last = heatT1[heatIndx].first; counter += heatT1[heatIndx].second; potential += heatT1[heatIndx].second; heatIndx++; } else { if (last > -1) counter += (potential - oppositePotential) * (coldT2[coldIndx].first - last); last = coldT2[coldIndx].first; if (coldT2[coldIndx].second > 0) counter -= coldT2[coldIndx].second; oppositePotential += coldT2[coldIndx].second; coldIndx++; } //printf("counter: %d, potential: %d\n",counter, potential); if (counter < 0) { isOK = false; break; } } if (!isOK) printf("NIE\n"); else printf("TAK\n"); } } |