#include<cstdio> #include<vector> #include<utility> #include<algorithm> typedef long long ll; int t; int n; std::vector< std::pair<int, int> > given; std::vector< std::pair<int, int> > expected; void input() { scanf("%d", &n); given.clear(); expected.clear(); int a, b, l; for (int i = 0; i < n; i++) { scanf("%d%d%d", &l, &a, &b); given.push_back(std::make_pair(a, l)); expected.push_back(std::make_pair(b, l)); } } bool is_sum_ok() { long long ex_sum = 0, gi_sum = 0; for (int i = 0; i < n; i++) { ex_sum += (ll)(expected[i].first) * expected[i].second; gi_sum += (ll)(given[i].first) * given[i].second; } return ex_sum == gi_sum; } bool cmp(std::pair<int, int> left, std::pair<int, int> right) { return left.first > right.first; } void sort_vects(bool asc) { if (asc) { std::sort(given.begin(), given.end()); std::sort(expected.begin(), expected.end()); } else { std::sort(given.begin(), given.end(), cmp); std::sort(expected.begin(), expected.end(), cmp); } } int max(int a, int b) { return a >= b ? a : b; } int min(int a, int b) { return a <= b ? a : b; } bool check_from_hottest() { // check heat and volume int i = 0, j = -1; // i for expected ll rest = 0; int vol_rest = 0; while (i < n) { ll cost = (ll)(expected[i].first) * expected[i].second; int vol = expected[i].second; while (rest < cost) { j++; if (j == n) { return false; } if (given[j].first <= expected[i].first) { //printf("%d < %d\n", given[j].first, expected[i].first); ll high = (ll)(vol_rest) * expected[i].first; int k = j; int v = vol_rest; while (k < n && v < vol) { //printf("v=%d vol=%d\n", v, vol); int vr = min(given[k].second, vol - v); high += (ll)(vr) * (expected[i].first - given[k].first); v += vr; k++; } if (high > rest || (k != j && v != vol)) { return false; } } rest += (ll)(given[j].first) * given[j].second; vol_rest += given[j].second; } //printf("i=%d rest=%lld vol_rest=%d j=%d\n", i, rest, vol_rest, j); rest -= cost; vol_rest -= vol; i++; } return true; } bool check_from_coldest() { // check heat and volume int i = 0, j = -1; // i for expected ll rest = 0; int vol_rest = 0; while (i < n) { ll cost = (ll)(expected[i].first) * expected[i].second; int vol = expected[i].second; while (rest < cost) { j++; if (j == n) { return false; } // if (given[j].first >= expected[i].first) { // ll low = (ll)(vol_rest) * expected[i].first; // if (vol > vol_rest) { // low -= (ll)(vol - vol_rest) * (given[j].first - expected[i].first); // } // if (low < rest) { // return false; // } // } if (given[j].first >= expected[i].first) { ll low = (ll)(vol_rest) * expected[i].first; int k = j; int v = vol_rest; while (k < n && v < vol) { int vr = min(given[k].second, vol - v); low -= (ll)(vr) * (given[k].first - expected[i].first); v += vr; k++; } if (low < rest || (k != j && v != vol)) { return false; } } rest += (ll)(given[j].first) * given[j].second; vol_rest += given[j].second; } rest -= cost; vol_rest -= vol; i++; } return true; } void print(std::vector< std::pair<int, int> > v) { for (int i = 0; i < n; i++) { printf("%d %d\n", v[i].first, v[i].second); } printf("---\n"); } bool solve() { if (!is_sum_ok()) { return false; } sort_vects(false); //print(given); //print(expected); if (!check_from_hottest()) { return false; } sort_vects(true); return check_from_coldest(); } int main() { scanf("%d", &t); while(t--) { input(); printf(solve() ? "TAK\n" : "NIE\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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include<cstdio> #include<vector> #include<utility> #include<algorithm> typedef long long ll; int t; int n; std::vector< std::pair<int, int> > given; std::vector< std::pair<int, int> > expected; void input() { scanf("%d", &n); given.clear(); expected.clear(); int a, b, l; for (int i = 0; i < n; i++) { scanf("%d%d%d", &l, &a, &b); given.push_back(std::make_pair(a, l)); expected.push_back(std::make_pair(b, l)); } } bool is_sum_ok() { long long ex_sum = 0, gi_sum = 0; for (int i = 0; i < n; i++) { ex_sum += (ll)(expected[i].first) * expected[i].second; gi_sum += (ll)(given[i].first) * given[i].second; } return ex_sum == gi_sum; } bool cmp(std::pair<int, int> left, std::pair<int, int> right) { return left.first > right.first; } void sort_vects(bool asc) { if (asc) { std::sort(given.begin(), given.end()); std::sort(expected.begin(), expected.end()); } else { std::sort(given.begin(), given.end(), cmp); std::sort(expected.begin(), expected.end(), cmp); } } int max(int a, int b) { return a >= b ? a : b; } int min(int a, int b) { return a <= b ? a : b; } bool check_from_hottest() { // check heat and volume int i = 0, j = -1; // i for expected ll rest = 0; int vol_rest = 0; while (i < n) { ll cost = (ll)(expected[i].first) * expected[i].second; int vol = expected[i].second; while (rest < cost) { j++; if (j == n) { return false; } if (given[j].first <= expected[i].first) { //printf("%d < %d\n", given[j].first, expected[i].first); ll high = (ll)(vol_rest) * expected[i].first; int k = j; int v = vol_rest; while (k < n && v < vol) { //printf("v=%d vol=%d\n", v, vol); int vr = min(given[k].second, vol - v); high += (ll)(vr) * (expected[i].first - given[k].first); v += vr; k++; } if (high > rest || (k != j && v != vol)) { return false; } } rest += (ll)(given[j].first) * given[j].second; vol_rest += given[j].second; } //printf("i=%d rest=%lld vol_rest=%d j=%d\n", i, rest, vol_rest, j); rest -= cost; vol_rest -= vol; i++; } return true; } bool check_from_coldest() { // check heat and volume int i = 0, j = -1; // i for expected ll rest = 0; int vol_rest = 0; while (i < n) { ll cost = (ll)(expected[i].first) * expected[i].second; int vol = expected[i].second; while (rest < cost) { j++; if (j == n) { return false; } // if (given[j].first >= expected[i].first) { // ll low = (ll)(vol_rest) * expected[i].first; // if (vol > vol_rest) { // low -= (ll)(vol - vol_rest) * (given[j].first - expected[i].first); // } // if (low < rest) { // return false; // } // } if (given[j].first >= expected[i].first) { ll low = (ll)(vol_rest) * expected[i].first; int k = j; int v = vol_rest; while (k < n && v < vol) { int vr = min(given[k].second, vol - v); low -= (ll)(vr) * (given[k].first - expected[i].first); v += vr; k++; } if (low < rest || (k != j && v != vol)) { return false; } } rest += (ll)(given[j].first) * given[j].second; vol_rest += given[j].second; } rest -= cost; vol_rest -= vol; i++; } return true; } void print(std::vector< std::pair<int, int> > v) { for (int i = 0; i < n; i++) { printf("%d %d\n", v[i].first, v[i].second); } printf("---\n"); } bool solve() { if (!is_sum_ok()) { return false; } sort_vects(false); //print(given); //print(expected); if (!check_from_hottest()) { return false; } sort_vects(true); return check_from_coldest(); } int main() { scanf("%d", &t); while(t--) { input(); printf(solve() ? "TAK\n" : "NIE\n"); } } |