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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct zad
{
	int p, r, q,z;
};

vector<zad> dane;

int poR(int x, int y)
{
	return dane[x].r > dane[y].r;
}

int poQ(int x, int y)
{
	return dane[x].q > dane[y].q;
}

int spr(int n)
{
	for (int i = 0;i < n;++i)
		if (dane[i].q <= dane[i].z)
			return 0;
	return 1;
}

int main()
{
	ios::sync_with_stdio(false);
	int n,m, e, t=0, a=0;
	vector<int> tabm;
	vector<int> N, G;
	
	cin >> n>>m;
	dane.resize(n);
	for (int i = 0;i < n;++i)
	{
		cin >> dane[i].r >> dane[i].q >> dane[i].p;
		N.push_back(i);
	}
	for (int i = 0;i < m;++i)
		tabm.push_back(-1);

	sort(N.begin(), N.end(),poR);

	make_heap(G.begin(), G.end(), poQ);

	while (G.size() > 0 || N.size() > 0)
	{
		while (N.size() > 0 && dane[N.back()].r <= t)
		{
			e = N.back();
			
			N.pop_back();
			G.push_back(e);
			push_heap(G.begin(), G.end(), poQ);

			//wyszukanie w tabm najmniejszego q
			int minq = -1;
			int in=-1;
			for (int i = 0;i < m;++i)
			{
				if (tabm[i] != -1)
				{
					if (minq == -1)
					{
						minq = dane[tabm[i]].q;
						in = tabm[i];
					}
					else
						if (dane[tabm[i]].q < minq)
						{
							minq = dane[tabm[i]].q;
							in = tabm[i];
						}
				}
			}
			
			if(in!=-1)
			if (dane[e].q > minq)
			{
				dane[in].p = t - dane[e].r;
				t = dane[e].r;
				if (dane[in].p > 0)
				{
					G.push_back(in);
					push_heap(G.begin(), G.end(), poQ);
				}
			}
		}
		if (G.size() == 0)
		{
			t = dane[N.back()].r;
			continue;
		}

		//sprawdzenie tabm czy jakies zadania sie nie skonczyly
		for (int i = 0;i < m;++i)
		{
			if (tabm[i] != -1)
			{
				if (dane[tabm[i]].z < t || dane[tabm[i]].p==0)
					tabm[i] = -1;
			}
		}
		//dodanie zadania
		for (int i = 0;i < m;++i)
		{
			if (tabm[i] == -1)
			{
				if (G.size())
				{
					e = G.front();
					pop_heap(G.begin(), G.end(), poQ);
					G.pop_back();
					tabm[i] = e;
					dane[e].z = t + dane[e].p;
				}
			}
		}
		//wyszukanie w tabm najmniejszej wartosci t+p
		int mint = -1;
		for (int i = 0;i < m;++i)
		{
			if (tabm[i] != -1)
			{
				if (mint == -1)
				{
					mint = t + dane[tabm[i]].p;
					a = tabm[i];
				}else
					if (t + dane[tabm[i]].p < mint)
					{
						mint = t + dane[tabm[i]].p;
						a = tabm[i];
				}
			}
		}
		e = a;
		t += dane[e].p;

	}
	if(spr(n))
		cout << "TAK\n";
	else cout << "NIE\n";
	return 0;
}