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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>


typedef std::pair<int,int> IntPair;
typedef std::pair<IntPair,int> Process;
//typedef std::pair<int, IntPair> SuspendedProcess;//margines, (pozostale obliczenie, deadline)

class WorkingProcess{
public:
	int delay;
	int toCount;
	int id;
	int operator< (const WorkingProcess & c) { return delay < c.delay;}
	WorkingProcess(int d, int c,int i):delay(d),toCount(c), id(i){};
	virtual ~WorkingProcess(){};
} ;

std::vector<Process> proceses;
std::list<WorkingProcess> working;
//std::priority_queue<SuspendedProcess> suspended;
std::list<WorkingProcess> suspended; //po kazdym ticku musi byc przetworzenie wszystkich procesow
int n,m;


void printState(){
	printf("WORKING: ");
	for(std::list<WorkingProcess>::iterator it = working.begin(); it != working.end(); it++){
		printf("(%d : %d %d) ",it->id, it->toCount,it->delay);
	}
	printf("\n");
	printf("WAITING: ");
	for(std::list<WorkingProcess>::iterator it = suspended.begin(); it != suspended.end(); it++){
		printf("(%d : %d %d) ",it->id, it->toCount,it->delay);
	}
	printf("\n");
}



std::list<WorkingProcess>::iterator getLowestDelay( std::list<WorkingProcess> & col){
	std::list<WorkingProcess>::iterator ret = col.begin();
	for(std::list<WorkingProcess>::iterator it = col.begin(); it != col.end(); it++){
		if (it->delay < ret->delay) ret = it;
	}
	return ret;
}

std::list<WorkingProcess>::iterator getBiggestDelay(std::list<WorkingProcess> & col){
	std::list<WorkingProcess>::iterator ret = col.begin();
	for(std::list<WorkingProcess>::iterator it = col.begin(); it != col.end(); it++){
		if (it->delay > ret->delay) ret = it;
	}
	return ret;
}

std::list<WorkingProcess>::iterator getFastestEnd(std::list<WorkingProcess> & col){
	std::list<WorkingProcess>::iterator ret = col.begin();
	for(std::list<WorkingProcess>::iterator it = col.begin(); it != col.end(); it++){
		if (it->toCount < ret->toCount) ret = it;
	}
	return ret;
}

int checkAndSwap(){
	std::list<WorkingProcess>::iterator toPut = getLowestDelay(suspended);
	std::list<WorkingProcess>::iterator toSuspend = getBiggestDelay(working);
	if (toPut != suspended.end() && toPut->delay < toSuspend->delay){
		WorkingProcess newWaiting = *toSuspend;
		WorkingProcess newWorking = *toPut;
		suspended.erase(toPut);
		working.erase(toSuspend);
		suspended.push_back(newWaiting);
		working.push_back(newWorking);
		return 1;
	}
	return 0;
}

void moveToExecution(){
	//printf("move\n");
	if (suspended.empty()) return;
	std::list<WorkingProcess>::iterator toPut = getLowestDelay(suspended);
	WorkingProcess newWorking = *toPut;
	suspended.erase(toPut);
	working.push_back(newWorking);
}

int clockShift(int sft){
	//printf("clockShift\n");
	/*
	for(std::list<WorkingProcess>::iterator it = working.begin(); it != working.end(); it++){
		it->toCount-=sft;
	}
	for(std::list<WorkingProcess>::iterator it = suspended.begin(); it != suspended.end(); it++){
		it->delay-=sft;
		if (it->delay < 0) return 1;
	}*/

	int step = 0;
	int change;
	do {
		change = 0;
		//working.sort();
		std::list<WorkingProcess>::iterator toEnd = getFastestEnd(working);
		//printf("working %d\n",toEnd->toCount);
		if (toEnd != working.end() && sft >= toEnd->toCount){
			//suspended.sort();
			working.erase(toEnd);
			step = toEnd->toCount;
			change = 1;
		} else {
			step = sft;
		}
		//printf("step %d %d change %d\n",step,sft,change);
		for(std::list<WorkingProcess>::iterator it = suspended.begin(); it != suspended.end(); it++){
			it->delay-=step;
			if (it->delay < 0) return 1;
		}
		for(std::list<WorkingProcess>::iterator it = working.begin(); it != working.end(); it++){
			it->toCount-=step;
		}
		while (m > (int)working.size() && !suspended.empty()) moveToExecution();
		sft -= step;
		while (checkAndSwap());
	} while(change);
	return 0;
}




int doStep(){
	int step = 0;

	//working.sort();
	std::list<WorkingProcess>::iterator toEnd = getFastestEnd(working);
	if (toEnd != working.end()){
		suspended.sort();
		working.erase(toEnd);
		step = toEnd->toCount;
	}
	//printf("step %d\n",step);fflush(stdout);
	for(std::list<WorkingProcess>::iterator it = suspended.begin(); it != suspended.end(); it++){
		it->delay-=step;
		if (it->delay < 0) return 1;
	}
	for(std::list<WorkingProcess>::iterator it = working.begin(); it != working.end(); it++){
		it->toCount-=step;
	}
	while (m > (int)working.size() && !suspended.empty()) moveToExecution();
	//printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	//printState();
	while(checkAndSwap());
	//printState();
	return 0;
}







int main(){
	int p,k,c;
	int clock = 0;
	int nextClock;
	int processToCheck = 0;
	scanf("%d %d",&n,&m);
	for(int i = 0; i < n; i++){
		scanf("%d %d %d", &p,&k,&c);
		proceses.push_back(Process(IntPair(p,k),c));
	}
	std::sort(proceses.begin(), proceses.end());

	while(processToCheck < (int)proceses.size()){

		Process pr = proceses[processToCheck++];
		nextClock = pr.first.first;
		//printf("###################################################################  clock %d ==> %d\n",clock,nextClock);
		if (clockShift(nextClock - clock) == 1){//ktorys proces ma ujemny czas oczekiwania
			printf("NIE\n");
			return 0;
		}
		//printf("BEFORE PUT ======>\n");
		//printState();
		if ((int)working.size() < m) {// po prostu wrzucamy do komputera
			working.push_back(WorkingProcess(pr.first.second - pr.first.first - pr.second,pr.second,processToCheck-1));
		} else {//wrzucamy do oczekujacych
			suspended.push_back(WorkingProcess(pr.first.second - pr.first.first - pr.second,pr.second,processToCheck-1));
		}
		if ((int)working.size() == m && !suspended.empty()) while(checkAndSwap()){};//po wrzuceniu nowego procesu przestawiamy oczekujace i dzialajace procesy
		//printf("AFTER PUT --------------------------->\n");
		//printState();
		clock = nextClock;
	};
	//printf("WLOZYL WSZYSTKIE\n");
	while(!suspended.empty()) if (doStep()) {printf("NIE\n");return 0;};
	printf("TAK\n");
	return 0;
}