#include<bits/stdc++.h> using namespace std; using lld = long long; struct State{ int val; int cnt; int del; State(void) {} State(int a, int b, int c): val(a), cnt(b), del(c) {} }; bool operator < (const State& a, const State& b){ return a.val < b.val; } vector<State> calc(vector<int>& l, vector<int>& a, vector<int>& b){ int n = l.size(); vector<pair<int,int> > x(n), y(n); for(int i=0;i<n;i++){ x[i] = {a[i],l[i]}; y[i] = {b[i],l[i]}; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); vector<State> t; while(!x.empty()){ int l = min(x.back().second, y.back().second); if(x.back().first != y.back().first) t.push_back(State(x.back().first, l, y.back().first-x.back().first)); x.back().second -= l; y.back().second -= l; while(!x.empty() && x.back().second == 0) x.pop_back(); while(!y.empty() && y.back().second == 0) y.pop_back(); } return t; } void solve1(priority_queue<State>& A, priority_queue<State>& B){ while(!A.empty() && !B.empty()){ State a = A.top(); State b = B.top(); A.pop(), B.pop(); if(a.cnt < b.cnt){ State x = b; b.cnt = a.cnt; x.cnt -= a.cnt; B.push(x); }else if(a.cnt > b.cnt){ State x = a; a.cnt = b.cnt; x.cnt -= b.cnt; A.push(x); } if(a.val >= b.val-1){ cout << "NIE\n"; return; } int p = min((b.val-a.val)/2, min(a.del, b.del)); a.del -= p, a.val += p; if(a.del > 0) A.push(a); b.del -= p, b.val -= p; if(b.del > 0) B.push(b); if(a.val > b.val){ cout << "NIE\n"; return; } while(!A.empty() && A.top().del == 0) A.pop(); while(!B.empty() && B.top().del == 0) B.pop(); } cout << (A.empty() && B.empty() ? "TAK" : "NIE") << "\n"; } void solve2(priority_queue<State>& A, priority_queue<State>& B){ while(!A.empty() && !B.empty()){ State a = A.top(); State b = B.top(); A.pop(), B.pop(); if(a.cnt < b.cnt){ State x = b; b.cnt = a.cnt; x.cnt -= a.cnt; B.push(x); }else if(a.cnt > b.cnt){ State x = a; a.cnt = b.cnt; x.cnt -= b.cnt; A.push(x); } if(a.val >= b.val){ cout << "NIE\n"; return; } int p = min(a.del, b.del); a.del -= p, a.val += p; if(a.del > 0) A.push(a); b.del -= p, b.val -= p; if(b.del > 0) B.push(b); if(a.val > b.val){ cout << "NIE\n"; return; } while(!A.empty() && A.top().del == 0) A.pop(); while(!B.empty() && B.top().del == 0) B.pop(); } cout << (A.empty() && B.empty() ? "TAK" : "NIE") << "\n"; } void query(void){ int n; cin >> n; vector<int> l(n), a(n), b(n); for(int i=0;i<n;i++) cin >> l[i] >> a[i] >> b[i]; vector<State> t = calc(l,a,b); priority_queue<State> A,B; for(auto& i : t) if(i.del > 0) A.push(i); else{ i.del = -i.del; B.push(i); } if(n <= 2000) solve1(A,B); else solve2(A,B); } int main(void){ ios_base::sync_with_stdio(false); cin.tie(NULL); int q; cin >> q; while(q--) query(); 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 | #include<bits/stdc++.h> using namespace std; using lld = long long; struct State{ int val; int cnt; int del; State(void) {} State(int a, int b, int c): val(a), cnt(b), del(c) {} }; bool operator < (const State& a, const State& b){ return a.val < b.val; } vector<State> calc(vector<int>& l, vector<int>& a, vector<int>& b){ int n = l.size(); vector<pair<int,int> > x(n), y(n); for(int i=0;i<n;i++){ x[i] = {a[i],l[i]}; y[i] = {b[i],l[i]}; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); vector<State> t; while(!x.empty()){ int l = min(x.back().second, y.back().second); if(x.back().first != y.back().first) t.push_back(State(x.back().first, l, y.back().first-x.back().first)); x.back().second -= l; y.back().second -= l; while(!x.empty() && x.back().second == 0) x.pop_back(); while(!y.empty() && y.back().second == 0) y.pop_back(); } return t; } void solve1(priority_queue<State>& A, priority_queue<State>& B){ while(!A.empty() && !B.empty()){ State a = A.top(); State b = B.top(); A.pop(), B.pop(); if(a.cnt < b.cnt){ State x = b; b.cnt = a.cnt; x.cnt -= a.cnt; B.push(x); }else if(a.cnt > b.cnt){ State x = a; a.cnt = b.cnt; x.cnt -= b.cnt; A.push(x); } if(a.val >= b.val-1){ cout << "NIE\n"; return; } int p = min((b.val-a.val)/2, min(a.del, b.del)); a.del -= p, a.val += p; if(a.del > 0) A.push(a); b.del -= p, b.val -= p; if(b.del > 0) B.push(b); if(a.val > b.val){ cout << "NIE\n"; return; } while(!A.empty() && A.top().del == 0) A.pop(); while(!B.empty() && B.top().del == 0) B.pop(); } cout << (A.empty() && B.empty() ? "TAK" : "NIE") << "\n"; } void solve2(priority_queue<State>& A, priority_queue<State>& B){ while(!A.empty() && !B.empty()){ State a = A.top(); State b = B.top(); A.pop(), B.pop(); if(a.cnt < b.cnt){ State x = b; b.cnt = a.cnt; x.cnt -= a.cnt; B.push(x); }else if(a.cnt > b.cnt){ State x = a; a.cnt = b.cnt; x.cnt -= b.cnt; A.push(x); } if(a.val >= b.val){ cout << "NIE\n"; return; } int p = min(a.del, b.del); a.del -= p, a.val += p; if(a.del > 0) A.push(a); b.del -= p, b.val -= p; if(b.del > 0) B.push(b); if(a.val > b.val){ cout << "NIE\n"; return; } while(!A.empty() && A.top().del == 0) A.pop(); while(!B.empty() && B.top().del == 0) B.pop(); } cout << (A.empty() && B.empty() ? "TAK" : "NIE") << "\n"; } void query(void){ int n; cin >> n; vector<int> l(n), a(n), b(n); for(int i=0;i<n;i++) cin >> l[i] >> a[i] >> b[i]; vector<State> t = calc(l,a,b); priority_queue<State> A,B; for(auto& i : t) if(i.del > 0) A.push(i); else{ i.del = -i.del; B.push(i); } if(n <= 2000) solve1(A,B); else solve2(A,B); } int main(void){ ios_base::sync_with_stdio(false); cin.tie(NULL); int q; cin >> q; while(q--) query(); return 0; } |