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
#include <iostream>
#include <vector>
#include <algorithm>

struct mug
{
	int liters;
	int currentTemp, expectedTemp;
};

int main()
{
	std::ios::sync_with_stdio(false);
	int n, k;
	int sumExpected, sumCurrent, maxTemp;
	std::vector<int> inc, dec;
	std::vector<mug> mugs;
	inc.reserve(1000000);
	dec.reserve(1000000);
	mugs.reserve(1000000);

	std::cin >> k;

	while (k--)
	{
		maxTemp = sumExpected = sumCurrent = 0;

		std::cin >> n;
		mugs.resize(n);
		for (int i = 0; i < n; ++i)
		{
			std::cin >> mugs[i].liters >> mugs[i].currentTemp >> mugs[i].expectedTemp;
			const auto& mug = mugs[i];
			
			sumExpected += mug.liters * mug.expectedTemp;
			sumCurrent += mug.liters * mug.currentTemp;

			if (mug.currentTemp > maxTemp)
				maxTemp = mug.currentTemp;
			if (mug.expectedTemp > maxTemp)
				maxTemp = mug.expectedTemp;
		}

		if (sumExpected != sumCurrent)
		{
			std::cout << "NIE" << std::endl;
			continue;
		}

		--maxTemp;
		inc.resize(maxTemp);
		dec.resize(maxTemp);
		std::fill(std::begin(inc), std::end(inc), 0);
		std::fill(std::begin(dec), std::end(dec), 0);

		for(const auto& mug : mugs)
		{
			if (mug.currentTemp > mug.expectedTemp)
			{
				for (int j = mug.expectedTemp - 1; j < mug.currentTemp - 1; ++j)
					dec[j] += mug.liters;
			}
			else if (mug.currentTemp < mug.expectedTemp)
			{
				for (int j = mug.currentTemp - 1; j < mug.expectedTemp - 1; ++j)
					inc[j] += mug.liters;
			}
		}

		int indexInc = 0, indexDec = 0;
		while (true)
		{
			while (indexInc < maxTemp && inc[indexInc] == 0)
				++indexInc;

			while (indexDec < maxTemp && dec[indexDec] == 0)
				++indexDec;

			if (indexInc == maxTemp && indexDec == maxTemp)
			{
				std::cout << "TAK" << std::endl;
				break;
			}

			if (indexInc > indexDec || indexInc == maxTemp || indexDec == maxTemp)
			{
				std::cout << "NIE" << std::endl;
				break;
			}

			const int litersToWarm = std::min(inc[indexInc], dec[indexDec]);
			inc[indexInc] -= litersToWarm;
			dec[indexDec] -= litersToWarm;
		}
	}

	return 0;
}