// Author: Kamil Nizinski // NOLINT(legal/copyright) #ifdef LOCAL #ifdef COLOR #include "bits/cp_local_color.h" #else #include "bits/cp_local.h" #endif #else #include "bits/stdc++.h" #define debug_arr(arr_name, first, last) #define debug(...) #define cerr if (false) cerr #define speed_of_cin_and_cout ios_base::sync_with_stdio(0), cin.tie(0) #define local if (false) #endif #define ft first #define sd second #define psb push_back #define sz(a) (static_cast<int>((a).size())) using namespace std; // NOLINT(build/namespaces) typedef int64_t LL; typedef uint64_t LLU; typedef long double LD; typedef pair<int, int> PII; // const int kMaxN = 100007; vector<PII> have, want; // {temperature, number of liters} bool solve() { int n; cin >> n; have.clear(); want.clear(); for (int i = 0; i < n; i++) { int l, a, b; cin >> l >> a >> b; have.emplace_back(a, l); want.emplace_back(b, l); } sort(have.begin(), have.end()); sort(want.begin(), want.end()); LL sum = LL{0}; for (int i = n - 1; i >= 0; i--) { int l = want[i].sd; while (l > 0) { int delta = min(l, have.back().sd); sum += LL{1} * delta * have.back().ft; have.back().sd -= delta; if (have.back().sd == 0) have.pop_back(); l -= delta; } sum -= LL{1} * want[i].ft * want[i].sd; if (sum < LL{0}) return false; } return (sum == LL{0}); } int main() { speed_of_cin_and_cout; int test_cases_num = 1; cin >> test_cases_num; for (int i = 1; i <= test_cases_num; i++) { local if (test_cases_num > 1) cerr << "Test #" << i << ":\n"; cout << (solve() ? "TAK\n" : "NIE\n"); local if (test_cases_num > 1) cerr << "End of test #" << i << ".\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 | // Author: Kamil Nizinski // NOLINT(legal/copyright) #ifdef LOCAL #ifdef COLOR #include "bits/cp_local_color.h" #else #include "bits/cp_local.h" #endif #else #include "bits/stdc++.h" #define debug_arr(arr_name, first, last) #define debug(...) #define cerr if (false) cerr #define speed_of_cin_and_cout ios_base::sync_with_stdio(0), cin.tie(0) #define local if (false) #endif #define ft first #define sd second #define psb push_back #define sz(a) (static_cast<int>((a).size())) using namespace std; // NOLINT(build/namespaces) typedef int64_t LL; typedef uint64_t LLU; typedef long double LD; typedef pair<int, int> PII; // const int kMaxN = 100007; vector<PII> have, want; // {temperature, number of liters} bool solve() { int n; cin >> n; have.clear(); want.clear(); for (int i = 0; i < n; i++) { int l, a, b; cin >> l >> a >> b; have.emplace_back(a, l); want.emplace_back(b, l); } sort(have.begin(), have.end()); sort(want.begin(), want.end()); LL sum = LL{0}; for (int i = n - 1; i >= 0; i--) { int l = want[i].sd; while (l > 0) { int delta = min(l, have.back().sd); sum += LL{1} * delta * have.back().ft; have.back().sd -= delta; if (have.back().sd == 0) have.pop_back(); l -= delta; } sum -= LL{1} * want[i].ft * want[i].sd; if (sum < LL{0}) return false; } return (sum == LL{0}); } int main() { speed_of_cin_and_cout; int test_cases_num = 1; cin >> test_cases_num; for (int i = 1; i <= test_cases_num; i++) { local if (test_cases_num > 1) cerr << "Test #" << i << ":\n"; cout << (solve() ? "TAK\n" : "NIE\n"); local if (test_cases_num > 1) cerr << "End of test #" << i << ".\n"; } return 0; } |