#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int t,n,m,m2,a,b,c,wsk;
long long jest,potrz;
const int mx=1e5+5;
struct szklanka{
long long poj,temp;
};
szklanka bylo[mx],bedzie[mx];
int main(){
ios::sync_with_stdio(false);
cin>>t;
for(int z=0;z<t;++z){
cin>>n;
for(int i=0;i<n;++i){
cin>>a>>b>>c;
bylo[i]={a,b};
bedzie[i]={a,c};
}
sort(bylo,bylo+n,[](szklanka a,szklanka b){return a.temp<b.temp;});
sort(bedzie,bedzie+n,[](szklanka a,szklanka b){return a.temp<b.temp;});
m=n;
m2=n;
wsk=0;
jest=0;
potrz=0;
for(int i=0;i<m2;++i){
potrz+=bedzie[i].poj*bedzie[i].temp;
long long zost=bedzie[i].poj;
while(zost>0){
while(bylo[wsk].poj==0){
++wsk;
if(wsk==m){
cout<<"NIE\n";
goto dalej;
}
}
if(bylo[wsk].poj>=zost){
jest+=zost*bylo[wsk].temp;
bylo[wsk].poj-=zost;
zost=0;
}
else{
jest+=bylo[wsk].poj*bylo[wsk].temp;
zost-=bylo[wsk].poj;
bylo[wsk].poj=0;
}
}
if(jest>potrz){
cout<<"NIE\n";
goto dalej;
}
}
if(jest!=potrz){
cout<<"NIE\n";
goto dalej;
}
cout<<"TAK\n";
dalej:;
}
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 | #include <iostream> #include<algorithm> #include<vector> using namespace std; int t,n,m,m2,a,b,c,wsk; long long jest,potrz; const int mx=1e5+5; struct szklanka{ long long poj,temp; }; szklanka bylo[mx],bedzie[mx]; int main(){ ios::sync_with_stdio(false); cin>>t; for(int z=0;z<t;++z){ cin>>n; for(int i=0;i<n;++i){ cin>>a>>b>>c; bylo[i]={a,b}; bedzie[i]={a,c}; } sort(bylo,bylo+n,[](szklanka a,szklanka b){return a.temp<b.temp;}); sort(bedzie,bedzie+n,[](szklanka a,szklanka b){return a.temp<b.temp;}); m=n; m2=n; wsk=0; jest=0; potrz=0; for(int i=0;i<m2;++i){ potrz+=bedzie[i].poj*bedzie[i].temp; long long zost=bedzie[i].poj; while(zost>0){ while(bylo[wsk].poj==0){ ++wsk; if(wsk==m){ cout<<"NIE\n"; goto dalej; } } if(bylo[wsk].poj>=zost){ jest+=zost*bylo[wsk].temp; bylo[wsk].poj-=zost; zost=0; } else{ jest+=bylo[wsk].poj*bylo[wsk].temp; zost-=bylo[wsk].poj; bylo[wsk].poj=0; } } if(jest>potrz){ cout<<"NIE\n"; goto dalej; } } if(jest!=potrz){ cout<<"NIE\n"; goto dalej; } cout<<"TAK\n"; dalej:; } return 0; } |
English