#include <iostream> #include <vector> #include <cmath> #include <set> #include <string> #include <fstream> #include <algorithm> #include <map> #include <utility> using ll = long long; struct Tea { int l; int a; int b; bool operator<(Tea const& other) const { return a < other.a || (a == other.a && b < other.b); } bool operator==(Tea const& other) const { return l == other.l && a == other.a && b == other.b; } }; void print(std::vector<ll> const& get, std::vector<ll> const& want, int end) { std::cout << "-----------------------" << std::endl; for (int i = 0; i<end; ++i) { std::cout << get[i] << " " << want[i] << std::endl; } } void print(std::set<Tea> const& first) { std::cout << "-----------------------" << std::endl; for (auto const& tea : first) { std::cout << tea.l << " " << tea.a << " " << tea.b << std::endl; } } int find_first(std::vector<ll> const& vec, int start, int end) { int result = end; for (int i = start; i < end; ++i) if (vec.at(i) != 0) return i; return result; } std::string balance(std::vector<ll>& inc, std::vector<ll>& dec, int start, int end) { if(dec[start] !=0 || inc[end-1] != 0) return "NIE"; int lowestInc = start, lowestDec = find_first(dec, start, end); for (int i = start; i<end; ++i) { if (dec[i] != 0) return "NIE"; if (inc[i] == 0) continue; while(inc[i] != 0) { int minValue = std::min(inc[i], dec[lowestDec]); inc[i] -= minValue; dec[lowestDec] -= minValue; lowestDec = find_first(dec, lowestDec, end); if (lowestDec == end) return "TAK"; } } std::string result = "TAK"; return result; } int main() { int tests; std::cin >> tests; for (int test = 0; test<tests;++test) { int n; std::cin >> n; int l,a,b; int maxValue = 0; std::map<std::pair<int,int>, int> increase; for (int i = 0;i<n;++i) { std::cin >> l >> a >> b; increase[{a,b}] += l; maxValue = std::max({maxValue, a, b}); } std::string result = ""; std::vector<ll> get(maxValue,0), want(maxValue,0); std::vector<Tea> teas; for (auto const& [a,value] : increase) { if (increase.count({a.second, a.first}) > 0) { ll minValue = std::min(value, increase[{a.second, a.first}]); increase[a] -= minValue; increase[{a.second, a.first}] -= minValue; } } for (auto const& [key, value] : increase) { if (value > 0 ) { teas.push_back({value, key.first, key.second}); } } long long int current{0}, required{0}; long long int aOnes{0}, bOnes{0}; for (auto const& tea : teas) { if (tea.a == 1) aOnes += tea.l; if (tea.b == 1) bOnes += tea.b; current += tea.l * tea.a; required += tea.l * tea.b; if (tea.a<tea.b) { for (int i=tea.a-1;i<tea.b-1;++i) get[i] += tea.l; } else { for(int i=tea.a-2;i>tea.b-2;--i) want[i] += tea.l; } } if (aOnes < bOnes ) result = "NIE"; else if (current != required) result = "NIE"; if (result.empty()) { int start = -1; int end = -1; for(int i = 0; i<get.size(); ++i) { if (get[i] !=0 && want[i] !=0) { ll minEl = std::min(get[i], want[i]); get[i] -= minEl; want[i] -= minEl; } if (get[i] != 0 || want[i] !=0) { end = i+1; if (start<0) start = i; } } if (start == -1 || end == -1) result = "TAK"; else result = balance(get, want, start, end); } std::cout << result<< std::endl; } 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 | #include <iostream> #include <vector> #include <cmath> #include <set> #include <string> #include <fstream> #include <algorithm> #include <map> #include <utility> using ll = long long; struct Tea { int l; int a; int b; bool operator<(Tea const& other) const { return a < other.a || (a == other.a && b < other.b); } bool operator==(Tea const& other) const { return l == other.l && a == other.a && b == other.b; } }; void print(std::vector<ll> const& get, std::vector<ll> const& want, int end) { std::cout << "-----------------------" << std::endl; for (int i = 0; i<end; ++i) { std::cout << get[i] << " " << want[i] << std::endl; } } void print(std::set<Tea> const& first) { std::cout << "-----------------------" << std::endl; for (auto const& tea : first) { std::cout << tea.l << " " << tea.a << " " << tea.b << std::endl; } } int find_first(std::vector<ll> const& vec, int start, int end) { int result = end; for (int i = start; i < end; ++i) if (vec.at(i) != 0) return i; return result; } std::string balance(std::vector<ll>& inc, std::vector<ll>& dec, int start, int end) { if(dec[start] !=0 || inc[end-1] != 0) return "NIE"; int lowestInc = start, lowestDec = find_first(dec, start, end); for (int i = start; i<end; ++i) { if (dec[i] != 0) return "NIE"; if (inc[i] == 0) continue; while(inc[i] != 0) { int minValue = std::min(inc[i], dec[lowestDec]); inc[i] -= minValue; dec[lowestDec] -= minValue; lowestDec = find_first(dec, lowestDec, end); if (lowestDec == end) return "TAK"; } } std::string result = "TAK"; return result; } int main() { int tests; std::cin >> tests; for (int test = 0; test<tests;++test) { int n; std::cin >> n; int l,a,b; int maxValue = 0; std::map<std::pair<int,int>, int> increase; for (int i = 0;i<n;++i) { std::cin >> l >> a >> b; increase[{a,b}] += l; maxValue = std::max({maxValue, a, b}); } std::string result = ""; std::vector<ll> get(maxValue,0), want(maxValue,0); std::vector<Tea> teas; for (auto const& [a,value] : increase) { if (increase.count({a.second, a.first}) > 0) { ll minValue = std::min(value, increase[{a.second, a.first}]); increase[a] -= minValue; increase[{a.second, a.first}] -= minValue; } } for (auto const& [key, value] : increase) { if (value > 0 ) { teas.push_back({value, key.first, key.second}); } } long long int current{0}, required{0}; long long int aOnes{0}, bOnes{0}; for (auto const& tea : teas) { if (tea.a == 1) aOnes += tea.l; if (tea.b == 1) bOnes += tea.b; current += tea.l * tea.a; required += tea.l * tea.b; if (tea.a<tea.b) { for (int i=tea.a-1;i<tea.b-1;++i) get[i] += tea.l; } else { for(int i=tea.a-2;i>tea.b-2;--i) want[i] += tea.l; } } if (aOnes < bOnes ) result = "NIE"; else if (current != required) result = "NIE"; if (result.empty()) { int start = -1; int end = -1; for(int i = 0; i<get.size(); ++i) { if (get[i] !=0 && want[i] !=0) { ll minEl = std::min(get[i], want[i]); get[i] -= minEl; want[i] -= minEl; } if (get[i] != 0 || want[i] !=0) { end = i+1; if (start<0) start = i; } } if (start == -1 || end == -1) result = "TAK"; else result = balance(get, want, start, end); } std::cout << result<< std::endl; } return 0; } |