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
#include <iostream>
#include <cstdint>
#include <vector>
#include <utility>
#include <algorithm>
#include <bitset>
#include <limits>

using namespace std;

typedef pair <int, int> kubek; // temperatura, objetosc

typedef pair<int64_t, int> en_vol; //energia objetosc

bool interp(en_vol f, en_vol g, en_vol x){
    //Jeśli potrzebna energia jest wieksza niż interpolowana energia z krzywej kubków danych, nie da sie
    return  x.first * (g.second-f.second) > (f.first *( g.second - x.second )+ g.first *( x.second - f.second ) );
 }


int main(){
    std::ios::sync_with_stdio(false);

    int t;
    cin>>t;
    vector<kubek> a,b;

    for (int rep=0; rep<t;rep++){
        a.clear();
        b.clear();

        int n;
        cin>>n;
        for (int i=0; i<n; i++){
            int v, ta, tb;
            cin>>v>>ta>>tb;
            a.push_back(make_pair(ta,v));
            b.push_back(make_pair(tb,v));
        }
        sort(begin(a),end(a),greater<kubek>());
        sort(begin(b),end(b),greater<kubek>());
        int64_t Ea =0, Eb=0;
        int va=0,vb=0;

        for (auto x:a){
            va += x.second;
            Ea += x.second*(int64_t)x.first;
        }
        for (auto x:b){
            vb += x.second;
            Eb += x.second*(int64_t)x.first;
        }
        if ( (va!=vb)||(Ea!=Eb) ){
            cout<<"NIE\n";
           // cerr<<"zasady podsawowe\n";
            continue;
        }
        Ea =0; Eb=0; va=0;vb=0;
        size_t it_a =0;
        en_vol pA = make_pair(0,0);
        en_vol pB = make_pair(0,0);
        en_vol pA_prev = make_pair(0,0);
        bool OK = true;
        for (size_t i=0;i<b.size(); i++){

            pB.first += b[i].first *(int64_t) b[i].second;
            pB.second += b[i].second;
            while( pA.second < pB.second ){
                pA_prev = pA;
                pA.first += a[it_a].first *(int64_t) a[it_a].second;
                pA.second += a[it_a].second;
                it_a++;
            }
            if ( interp(pA_prev, pA,pB) ){
                OK=false;
                break;
            }
        }
        cout<<(OK?"TAK":"NIE")<<endl;

    }



    return 0;
}