#include <bits/stdc++.h>
#define MSC pair <long double, long double>
using namespace std;
const long double EPS=(long double)1/(1000000000);
map <long long, long long> aa;
map <long long, long long> bb;
vector <MSC> a;
vector <MSC> b;
int q, n;
bool succes;
long long s1, s2;
bool compare(long double a, long double b){
return ((long double)(abs(a+(-b))))>EPS;
}
inline long double tem(MSC a, MSC b){
return (a.first*a.second+b.first*b.second)/(a.second+b.second);
}
inline long double toTake(MSC a, MSC b, long double expected){
return (a.second*a.first-expected*a.second)/(expected-b.first);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>q;
while (q--)
{
a.clear(), b.clear(), aa.clear(), bb.clear(), s1=s2=0, succes=true;;
cin>>n;
while (n--)
{
long long z, x, y;
cin>>z>>x>>y;
aa[x]+=z, bb[y]+=z, s1+=z*x, s2+=z*y;
}
if (s1!=s2)
{
cout<<"NIE\n";
continue;
}
for (auto i: aa)
a.push_back(i);
for (auto i: bb)
b.push_back(i);
for (int i=b.size()-1; i>=0; i--)
{
while (compare(a.back().first, b.back().first))
{
/*cout<<"0: "<<endl;
for (auto i: a)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;
for (auto i: b)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;*/
if (a.size()==1)
{
succes=false;
break;
}
if (tem(a.back(), a[a.size()-2])>=b.back().first)
{
MSC tmp=make_pair(tem(a.back(), a[a.size()-2]), a.back().second+a[a.size()-2].second);
a.pop_back(), a.pop_back(), a.push_back(tmp);
}
else
{
long double x=toTake(a.back(), a[a.size()-2], b.back().first);
a[a.size()-1]=make_pair(b.back().first, a[a.size()-1].second+x), a[a.size()-2].second-=x;
}
}
/*cout<<"1: "<<endl;
for (auto i: a)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;
for (auto i: b)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;*/
if (compare(a.back().first, b.back().first))
succes=false;
if (a.back().second<b.back().second-EPS)
succes=false;
if (!succes)
break;
if (!compare(a.back().second, b.back().second))
a.pop_back(), b.pop_back();
else
a[a.size()-1].second-=b.back().second, b.pop_back();
/*cout<<"2: "<<endl;
for (auto i: a)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;
for (auto i: b)
cout<<i.first<<" "<<i.second<<endl;
cout<<endl;
cout<<endl;*/
}
if (succes)
cout<<"TAK\n";
else
cout<<"NIE\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 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 | #include <bits/stdc++.h> #define MSC pair <long double, long double> using namespace std; const long double EPS=(long double)1/(1000000000); map <long long, long long> aa; map <long long, long long> bb; vector <MSC> a; vector <MSC> b; int q, n; bool succes; long long s1, s2; bool compare(long double a, long double b){ return ((long double)(abs(a+(-b))))>EPS; } inline long double tem(MSC a, MSC b){ return (a.first*a.second+b.first*b.second)/(a.second+b.second); } inline long double toTake(MSC a, MSC b, long double expected){ return (a.second*a.first-expected*a.second)/(expected-b.first); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>q; while (q--) { a.clear(), b.clear(), aa.clear(), bb.clear(), s1=s2=0, succes=true;; cin>>n; while (n--) { long long z, x, y; cin>>z>>x>>y; aa[x]+=z, bb[y]+=z, s1+=z*x, s2+=z*y; } if (s1!=s2) { cout<<"NIE\n"; continue; } for (auto i: aa) a.push_back(i); for (auto i: bb) b.push_back(i); for (int i=b.size()-1; i>=0; i--) { while (compare(a.back().first, b.back().first)) { /*cout<<"0: "<<endl; for (auto i: a) cout<<i.first<<" "<<i.second<<endl; cout<<endl; for (auto i: b) cout<<i.first<<" "<<i.second<<endl; cout<<endl;*/ if (a.size()==1) { succes=false; break; } if (tem(a.back(), a[a.size()-2])>=b.back().first) { MSC tmp=make_pair(tem(a.back(), a[a.size()-2]), a.back().second+a[a.size()-2].second); a.pop_back(), a.pop_back(), a.push_back(tmp); } else { long double x=toTake(a.back(), a[a.size()-2], b.back().first); a[a.size()-1]=make_pair(b.back().first, a[a.size()-1].second+x), a[a.size()-2].second-=x; } } /*cout<<"1: "<<endl; for (auto i: a) cout<<i.first<<" "<<i.second<<endl; cout<<endl; for (auto i: b) cout<<i.first<<" "<<i.second<<endl; cout<<endl;*/ if (compare(a.back().first, b.back().first)) succes=false; if (a.back().second<b.back().second-EPS) succes=false; if (!succes) break; if (!compare(a.back().second, b.back().second)) a.pop_back(), b.pop_back(); else a[a.size()-1].second-=b.back().second, b.pop_back(); /*cout<<"2: "<<endl; for (auto i: a) cout<<i.first<<" "<<i.second<<endl; cout<<endl; for (auto i: b) cout<<i.first<<" "<<i.second<<endl; cout<<endl; cout<<endl;*/ } if (succes) cout<<"TAK\n"; else cout<<"NIE\n"; } return 0; } |
English