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
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

struct Point{
    int start, time, ending, delay, index;
    bool inprocess;
    Point(int a, int b, int c, int i){
        start=a;
        ending=b;
        time=c;
        delay=b-c;
        inprocess=0;
        index=i;
    }
};

bool myfunction(Point* a, Point* b){ return (a->delay < b->delay); }

int main(){
    int n,m;
    cin >> n >> m;
    vector <Point * > points;
    set<int> startintime;
    for(int i=0; i<n; i++){
        int a, b, c;
        cin >> a >> b >> c;
        points.push_back(new Point(a,b,c,i));
        startintime.insert(points[i]->start);
    }
    int firstendintime = -1;
    set<int>::iterator it=startintime.begin();
    int now=0, previous;
    bool possible=1;
    while(it != startintime.end() || firstendintime!= -1){
        previous = now;
        now = *it;
        if((firstendintime!= -1 && firstendintime < now) || it == startintime.end()){
                now = firstendintime;
                firstendintime = -1;
        } else{
            it++;
        }
        for(int i=0; i<points.size(); i++){
            Point* nowy = points[i];
            if(nowy->inprocess){
                nowy->inprocess=0;
                nowy->time-=(now - previous);
                if(nowy->time == 0){
                    points.erase(points.begin()+i);
                    --i;
                    continue;
                }
            } else{
                nowy->delay-=(now-previous);
            }
        }
        sort(points.begin(), points.end(), myfunction);
        int inprocesscount=0, minn=3000000;
        for(int i=0; i<points.size() && inprocesscount<m; i++){
            if(points[i]->delay < 0){
                possible=0;
                break;
            }
            if(points[i]->start <= now){
                ++inprocesscount;
                points[i]->inprocess=1;
                minn = min(minn, points[i]->time+now);
            }
        }
        if(minn != 3000000) firstendintime = minn;
        if(!possible) break;
    }
    if(possible) cout << "TAK\n";
    else cout << "NIE\n";
}