#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long lld; typedef unsigned long long llu; typedef double lff; typedef long double lf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define pb push_back #define mp make_pair #define dd second #define ff first #define sz size() #define For(i,s,a) for(lld i=(lld)s;i<(lld)a;++i) pair<lf,lld> m[100001], mi[100001]; const lf eps = 0.00000000001; bool cmp(pair<lf,lld>a, pair<lf,lld>b){ return a.dd < b.dd || (a.dd == b.dd && round(a.ff) < round(b.ff)); } int bin(int where, int k, lld x){ while(where < k && (m[where].dd - x < 0 || (m[where].ff < eps))) ++where; if(where < k) return where; return -1; } //binsrch vs amortized constexpr bool debug = 0, hold = 0; void solve(){ int a; scanf("%d", &a); For(i,0,a){ int g,h,k; scanf("%d%d%d", &g,&h,&k); m[i] = mp(g,h); mi[i] = mp(g,k); } sort(m,m+a,cmp); sort(mi,mi+a,cmp); int i = 0, j = 0, where = 0; while(i<a && j<a){ if(m[i].dd - mi[j].dd ==0){ lf sub = min(m[i].ff, mi[j].ff); m[i].ff -= sub; mi[j].ff -=sub; } if(mi[j].ff - 0 < eps){ ++j;continue;} if(m[i].ff - 0 < eps){ ++i;continue;} where = bin(where,a, mi[j].dd); if(debug){ cout<<i<<" "<<j<<" "<<where<<endl; For(k,0,a) cout<<m[k].ff<<" "<<m[k].dd<<" "<<mi[k].ff<<" "<<mi[k].dd<<endl; puts(""); } if(where == -1){puts("NIE");return;} if(hold) getchar(); lld bot = m[where].dd - m[i].dd; if(m[where].dd - mi[j].dd == 0){ lf sub = min(m[where].ff, mi[j].ff); m[where].ff -= sub; mi[j].ff -=sub; } if(mi[j].ff - 0 < eps){ ++j;continue;} if(m[where].ff - 0 < eps){ continue;} if(bot == 0){puts("NIE");return;} //wtf if(debug) cout<<i<<" "<<j<<" "<<where<<" "<<m[i].ff<<" "<<m[i].dd<<" "<<mi[j].ff<<" "<<mi[j].dd<<" "<<m[where].ff<<" "<<m[where].dd<<" "; lf part = mi[j].ff / bot * (m[where].dd - mi[j].dd); lf scal1, scal2; scal1 = min((lf)1.0, m[i].ff / part); scal2 = min((lf)1.0, m[where].ff / (mi[j].ff - part)); part *= min(scal1, scal2); if(debug) cout<<part<<" A! "<<scal1<<" "<<scal2<<endl; m[i].ff -= part; m[where].ff -= (mi[j].ff * min(scal1, scal2) - part); mi[j].ff *= 1.0 - min(scal1, scal2); } puts("TAK"); } int32_t main(void){ int t; scanf("%d", &t); while(t--)solve(); } /* 5 2 2 1 4 2 5 2 2 1 4 3 1 5 4 2 1 5 7 1 7 5 2 1 4 1 1 2 5 3 2 6 4 1 2 3 3 4 5 */
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 | #include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long lld; typedef unsigned long long llu; typedef double lff; typedef long double lf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define pb push_back #define mp make_pair #define dd second #define ff first #define sz size() #define For(i,s,a) for(lld i=(lld)s;i<(lld)a;++i) pair<lf,lld> m[100001], mi[100001]; const lf eps = 0.00000000001; bool cmp(pair<lf,lld>a, pair<lf,lld>b){ return a.dd < b.dd || (a.dd == b.dd && round(a.ff) < round(b.ff)); } int bin(int where, int k, lld x){ while(where < k && (m[where].dd - x < 0 || (m[where].ff < eps))) ++where; if(where < k) return where; return -1; } //binsrch vs amortized constexpr bool debug = 0, hold = 0; void solve(){ int a; scanf("%d", &a); For(i,0,a){ int g,h,k; scanf("%d%d%d", &g,&h,&k); m[i] = mp(g,h); mi[i] = mp(g,k); } sort(m,m+a,cmp); sort(mi,mi+a,cmp); int i = 0, j = 0, where = 0; while(i<a && j<a){ if(m[i].dd - mi[j].dd ==0){ lf sub = min(m[i].ff, mi[j].ff); m[i].ff -= sub; mi[j].ff -=sub; } if(mi[j].ff - 0 < eps){ ++j;continue;} if(m[i].ff - 0 < eps){ ++i;continue;} where = bin(where,a, mi[j].dd); if(debug){ cout<<i<<" "<<j<<" "<<where<<endl; For(k,0,a) cout<<m[k].ff<<" "<<m[k].dd<<" "<<mi[k].ff<<" "<<mi[k].dd<<endl; puts(""); } if(where == -1){puts("NIE");return;} if(hold) getchar(); lld bot = m[where].dd - m[i].dd; if(m[where].dd - mi[j].dd == 0){ lf sub = min(m[where].ff, mi[j].ff); m[where].ff -= sub; mi[j].ff -=sub; } if(mi[j].ff - 0 < eps){ ++j;continue;} if(m[where].ff - 0 < eps){ continue;} if(bot == 0){puts("NIE");return;} //wtf if(debug) cout<<i<<" "<<j<<" "<<where<<" "<<m[i].ff<<" "<<m[i].dd<<" "<<mi[j].ff<<" "<<mi[j].dd<<" "<<m[where].ff<<" "<<m[where].dd<<" "; lf part = mi[j].ff / bot * (m[where].dd - mi[j].dd); lf scal1, scal2; scal1 = min((lf)1.0, m[i].ff / part); scal2 = min((lf)1.0, m[where].ff / (mi[j].ff - part)); part *= min(scal1, scal2); if(debug) cout<<part<<" A! "<<scal1<<" "<<scal2<<endl; m[i].ff -= part; m[where].ff -= (mi[j].ff * min(scal1, scal2) - part); mi[j].ff *= 1.0 - min(scal1, scal2); } puts("TAK"); } int32_t main(void){ int t; scanf("%d", &t); while(t--)solve(); } /* 5 2 2 1 4 2 5 2 2 1 4 3 1 5 4 2 1 5 7 1 7 5 2 1 4 1 1 2 5 3 2 6 4 1 2 3 3 4 5 */ |