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
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <list>

using namespace std;

struct Item
{
   int taskId = 0;
   int margin = 0;
   int exectime = 0;
   int totalRemoved = 0;

//   Item(int task, int mar, int exe)
//    : taskId(task), margin(mar), exectime(exe)
//   {
//   }
//   Item(const Item&) = default;

   int MinMargin(int total)
   {
      int min = margin - (total - totalRemoved);
      if(min < 0)
         return 0;
      return min;
   }

   void CalculateMinimumMarginRemove(int total)
   {
      if(exectime < total)
         totalRemoved = total - exectime;

      margin -= totalRemoved;
   }

   void ShrinkMargin(int shrink)
   {
      totalRemoved += shrink;
      margin -= shrink;
   }

   void RemoveToTotal(int total)
   {
      if(totalRemoved < total)
         exectime -= total - totalRemoved;

      totalRemoved = 0;
   }

   bool operator < (const Item& item)
   {
      return margin < item.margin;
   }

   bool operator == (const Item& item)
   {
      return taskId == item.taskId;
   }
};

struct TimeEvent
{
   int taskId;
   int eventTime;

   bool operator < (const TimeEvent& item)
   {
      return eventTime < item.eventTime;
   }
};

int CalcMarginSum(list<Item> Items)
{
   int sum = 0;
   for(Item& item: Items)
      sum += item.margin;

   return sum;
}

int main()
{
   list<TimeEvent> StartTaskEvents;
   list<TimeEvent> EndTaskEvents;
   list<Item> ProcessedItem;
   Item Items[100];
   int finishedItems = 0;
   int startedItems = 0;

   int task, proc;
   cin>>task>>proc;

   for(int i = 0; i < task; i++)
   {
      int start,end,calc;
      cin>>start>>end>>calc;

      Items[i].taskId = i;
      Items[i].exectime = calc;
      Items[i].margin = end-start-calc;

      TimeEvent startevent;
      startevent.taskId = i;
      startevent.eventTime = start;
      StartTaskEvents.push_back(startevent);

      TimeEvent endevent;
      endevent.taskId = i;
      endevent.eventTime = end;
      EndTaskEvents.push_back(endevent);
   }

   StartTaskEvents.sort();
   EndTaskEvents.sort();

   list<TimeEvent>::iterator sit = StartTaskEvents.begin();
   list<TimeEvent>::iterator eit = EndTaskEvents.begin();

   int currtime = 0;
   while(eit != EndTaskEvents.end())
   {
      //stop all task for current time
      for(; eit != EndTaskEvents.end() && eit->eventTime == currtime; eit++ )
      {
         //stop task, means remove element where
         ProcessedItem.remove(Items[eit->taskId]);
         finishedItems++;
         startedItems--;
      }
      if(eit == EndTaskEvents.end())
         break;

      int nextTime = eit->eventTime;


      //start all task for current time
      for(; sit != StartTaskEvents.end() && sit->eventTime == currtime; sit++ )
      {
         //start task by placing it in ProcessedItems
         int tid = sit->taskId;
         Item item = Items[sit->taskId];
         ProcessedItem.push_back(Items[sit->taskId]);
         startedItems++;
      }

      if(sit != StartTaskEvents.end() && sit->eventTime < nextTime)
         nextTime = sit->eventTime;

      int marginSum = CalcMarginSum(ProcessedItem);
      int totalToRemoveFromeach = nextTime - currtime;
      int awaitingTask = ProcessedItem.size() - proc;
      if(awaitingTask < 0)
         awaitingTask = 0;
      int destinationMargin = marginSum - totalToRemoveFromeach * awaitingTask;

      for(Item& item : ProcessedItem)
      {
         item.CalculateMinimumMarginRemove(totalToRemoveFromeach);
      }

      ProcessedItem.sort();
      ProcessedItem.reverse();

      //wygladzanie
      int stillToRemove = CalcMarginSum(ProcessedItem) - destinationMargin;
      for(list<Item>::iterator ToIt = ProcessedItem.begin(); ToIt != ProcessedItem.end() && stillToRemove > 0; ToIt++)
      {
         int min_value = ToIt->margin;
         for(list<Item>::iterator it = ProcessedItem.begin(); it != ToIt && stillToRemove > 0; it++)
         {
            int destMargin = it->MinMargin(totalToRemoveFromeach);
            if(destMargin < min_value)
               destMargin = min_value;

            int toremove = it->margin - destMargin;
            if(toremove > stillToRemove)
               toremove = stillToRemove;

            stillToRemove -= toremove;
            it->ShrinkMargin(toremove);
         }
      }
      {//last for every
         int min_value = 0;
         for(list<Item>::iterator it = ProcessedItem.begin(); it != ProcessedItem.end() && stillToRemove > 0; it++)
         {
            int destMargin = it->MinMargin(totalToRemoveFromeach);
            if(destMargin < min_value)
               destMargin = min_value;

            int toremove = it->margin - destMargin;
            if(toremove > stillToRemove)
               toremove = stillToRemove;

            stillToRemove -= toremove;
            it->ShrinkMargin(toremove);
         }
      }

      if(stillToRemove > 0)
      {
         cout<<"NIE";
         return 0;
      }


      //shrink exe time
      for(Item& item : ProcessedItem)
      {
         item.RemoveToTotal(totalToRemoveFromeach);
      }

      currtime = nextTime;
   }

   cout<<"TAK";
   return 0;
}