#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
void single_case() {
int n;
cin >> n;
vector< pair<int, int> > akt, cel; // (temperatura, objetosc)
int l, a, b;
for (int i = 0; i < n; i++) {
cin >> l >> a >> b;
akt.push_back( pair<int,int>(a, l) );
cel.push_back( pair<int,int>(b, l) );
}
sort(akt.begin(), akt.end());
sort(cel.begin(), cel.end());
long long bank = 0;
long long masa = 0;
for(int i = n-1; i>=0; i--) {
while(akt.size()>0 && masa + akt.back().second < cel.back().second ) {
bank += akt.back().first * akt.back().second;
masa += akt.back().second;
akt.pop_back();
}
if(masa < cel.back().second && akt.size()>0) {
int dm = cel.back().second - masa;
masa += dm;
bank += akt.back().first * dm;
akt.back().second -= dm;
}
bank -= cel.back().first * cel.back().second;
masa -= cel.back().second;
if(bank < 0 || masa < 0) {
cout << "NIE" << endl;
return;
}
cel.pop_back();
}
if(bank == 0 && masa == 0)
cout << "TAK" << endl;
else
cout << "NIE" << endl;
}
int main() {
int t;
cin >> t;
for(int it = 0; it < t; it++)
single_case();
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 | #include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; void single_case() { int n; cin >> n; vector< pair<int, int> > akt, cel; // (temperatura, objetosc) int l, a, b; for (int i = 0; i < n; i++) { cin >> l >> a >> b; akt.push_back( pair<int,int>(a, l) ); cel.push_back( pair<int,int>(b, l) ); } sort(akt.begin(), akt.end()); sort(cel.begin(), cel.end()); long long bank = 0; long long masa = 0; for(int i = n-1; i>=0; i--) { while(akt.size()>0 && masa + akt.back().second < cel.back().second ) { bank += akt.back().first * akt.back().second; masa += akt.back().second; akt.pop_back(); } if(masa < cel.back().second && akt.size()>0) { int dm = cel.back().second - masa; masa += dm; bank += akt.back().first * dm; akt.back().second -= dm; } bank -= cel.back().first * cel.back().second; masa -= cel.back().second; if(bank < 0 || masa < 0) { cout << "NIE" << endl; return; } cel.pop_back(); } if(bank == 0 && masa == 0) cout << "TAK" << endl; else cout << "NIE" << endl; } int main() { int t; cin >> t; for(int it = 0; it < t; it++) single_case(); return 0; } |
English