//Piotr Golda #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <stdexcept> #include <exception> #include <algorithm> struct Task; struct TaskGroup; struct TaskEvent; struct Overlap; class priority_queue_expanded; int N, M; std::vector< Task > Tasks; std::vector< TaskEvent > TaskEvents; struct Overlap { int startTime; int finishTime; int overlap; Overlap(int startTime, int finishTime, int overlap) : startTime(startTime), finishTime(finishTime), overlap(overlap) { } bool operator<(const Overlap& rhs) const { if (finishTime - overlap < rhs.finishTime - rhs.overlap) return false; if (finishTime - overlap > rhs.finishTime - rhs.overlap) return true; return finishTime > rhs.finishTime; } }; class priority_queue_expanded : public std::priority_queue < Overlap > { public: Overlap FindFirstBigger(const Overlap& overlap) { if (c.empty()) { throw std::runtime_error("FindFirstBigger on empty queue!"); } Overlap best{ c[0] }; for (const Overlap& ov : c) { int ovPt = ov.finishTime - ov.overlap; int overlapPt = overlap.finishTime - overlap.overlap; int bestPt = best.finishTime - best.overlap; if (ovPt > overlapPt && ovPt < bestPt) { best = ov; } else if (ovPt > overlapPt && ovPt == bestPt && ov.startTime < best.startTime) { best = ov; } } return best; } }; std::deque< Overlap > Overlaps; priority_queue_expanded OverlapQueue; struct Task { int startTime; int finishTime; int execTime; Task( int startTime, int finishTime, int execTime) : startTime(startTime), finishTime(finishTime), execTime(execTime) { } }; struct TaskGroup { int startTime; int finishTime; int amount; TaskGroup(int startTime, int finishTime, int amount = 0) : startTime(startTime), finishTime(finishTime), amount(amount) { } }; std::deque< TaskGroup > TaskGroups; enum class EvntType : char { Finish = 0, Start = 1 }; struct TaskEvent { int ID; EvntType type; int value; TaskEvent(int ID, EvntType type, int value) : ID(ID), type(type), value(value) { } bool operator<(const TaskEvent& rhs) const { if (value < rhs.value) return true; if (value > rhs.value) return false; return type < rhs.type; } }; void GetInput() { std::cin >> N; std::cin >> M; Tasks.reserve(N); TaskEvents.reserve(2*N); int a, b, c; for (int i = 0; i < N; ++i) { std::cin >> a >> b >> c; Tasks.emplace_back(a, b, c); } } void InitializeContainers() { for (int i = 0; i < Tasks.size(); ++i) { auto& task = Tasks[i]; TaskEvents.emplace_back(i, EvntType::Start, task.startTime); TaskEvents.emplace_back(i, EvntType::Finish, task.finishTime); if (task.finishTime - task.startTime - task.execTime < 0) throw std::runtime_error("Wrong input data!"); if (task.finishTime - task.startTime - task.execTime != 0) { Overlaps.emplace_back(task.startTime, task.finishTime, task.finishTime - task.startTime - task.execTime); } } std::sort(TaskEvents.begin(), TaskEvents.end()); std::sort(Overlaps.begin(), Overlaps.end(), [](const Overlap& lhs, const Overlap& rhs) { return lhs.startTime < rhs.startTime; }); int taskAmount{ 1 }; for (int i = 1; i < TaskEvents.size(); ++i) { const auto& evnt = TaskEvents[i]; if (TaskEvents[i - 1].value != evnt.value) { TaskGroups.emplace_back(TaskEvents[i - 1].value, evnt.value, taskAmount); } if (evnt.type == EvntType::Start) { taskAmount++; } else if (evnt.type == EvntType::Finish) { taskAmount--; } } } void SliceGroup(TaskGroup& group, int slicePt) { if ( slicePt >= group.finishTime || slicePt <= group.startTime) throw std::runtime_error("Wrong group slicing!"); TaskGroups.emplace_front( slicePt, group.finishTime, group.amount ); group.finishTime = slicePt; } bool Analize() { while (!TaskGroups.empty()) { auto group = TaskGroups.front(); TaskGroups.pop_front(); while ( !Overlaps.empty() && Overlaps[0].startTime <= group.startTime) { if (Overlaps[0].finishTime > group.startTime) OverlapQueue.push(Overlaps[0]); Overlaps.pop_front(); } while (group.amount > M) { if (OverlapQueue.empty()) return false; Overlap overlap = OverlapQueue.top(); OverlapQueue.pop(); if (overlap.finishTime <= group.startTime) continue; if (!OverlapQueue.empty() && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) < group.finishTime && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) > group.startTime && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) > overlap.finishTime - overlap.overlap && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) < overlap.finishTime) { int slicePt = OverlapQueue.top().finishTime - OverlapQueue.top().overlap; int newOv = slicePt - (overlap.finishTime - overlap.overlap); if (overlap.overlap - newOv <= 0) throw std::runtime_error("Somethings wrong in overlaps slicing!"); Overlaps.emplace_front(slicePt, overlap.finishTime, overlap.overlap - newOv); overlap.finishTime = slicePt; overlap.overlap = newOv; SliceGroup(group, slicePt); if (overlap.finishTime <= group.startTime) { throw std::runtime_error("Wrong after overlap slicing"); } } int preprevOv = overlap.overlap; overlap.overlap = std::min(overlap.finishTime - std::max( overlap.startTime, group.startTime ), overlap.overlap); if (overlap.overlap == 0) throw std::runtime_error("Somethings wrong in group slicing after using full overlap."); int prevOv = overlap.overlap; overlap.overlap -= (group.finishTime - group.startTime); if (overlap.overlap > 0) { overlap.startTime = group.finishTime; Overlaps.push_front(overlap); } else if (overlap.overlap < 0) { SliceGroup(group, group.finishTime + overlap.overlap); } --(group.amount); } } return true; } int main() { try { GetInput(); InitializeContainers(); if (Analize()) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } return 0; }
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | //Piotr Golda #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <stdexcept> #include <exception> #include <algorithm> struct Task; struct TaskGroup; struct TaskEvent; struct Overlap; class priority_queue_expanded; int N, M; std::vector< Task > Tasks; std::vector< TaskEvent > TaskEvents; struct Overlap { int startTime; int finishTime; int overlap; Overlap(int startTime, int finishTime, int overlap) : startTime(startTime), finishTime(finishTime), overlap(overlap) { } bool operator<(const Overlap& rhs) const { if (finishTime - overlap < rhs.finishTime - rhs.overlap) return false; if (finishTime - overlap > rhs.finishTime - rhs.overlap) return true; return finishTime > rhs.finishTime; } }; class priority_queue_expanded : public std::priority_queue < Overlap > { public: Overlap FindFirstBigger(const Overlap& overlap) { if (c.empty()) { throw std::runtime_error("FindFirstBigger on empty queue!"); } Overlap best{ c[0] }; for (const Overlap& ov : c) { int ovPt = ov.finishTime - ov.overlap; int overlapPt = overlap.finishTime - overlap.overlap; int bestPt = best.finishTime - best.overlap; if (ovPt > overlapPt && ovPt < bestPt) { best = ov; } else if (ovPt > overlapPt && ovPt == bestPt && ov.startTime < best.startTime) { best = ov; } } return best; } }; std::deque< Overlap > Overlaps; priority_queue_expanded OverlapQueue; struct Task { int startTime; int finishTime; int execTime; Task( int startTime, int finishTime, int execTime) : startTime(startTime), finishTime(finishTime), execTime(execTime) { } }; struct TaskGroup { int startTime; int finishTime; int amount; TaskGroup(int startTime, int finishTime, int amount = 0) : startTime(startTime), finishTime(finishTime), amount(amount) { } }; std::deque< TaskGroup > TaskGroups; enum class EvntType : char { Finish = 0, Start = 1 }; struct TaskEvent { int ID; EvntType type; int value; TaskEvent(int ID, EvntType type, int value) : ID(ID), type(type), value(value) { } bool operator<(const TaskEvent& rhs) const { if (value < rhs.value) return true; if (value > rhs.value) return false; return type < rhs.type; } }; void GetInput() { std::cin >> N; std::cin >> M; Tasks.reserve(N); TaskEvents.reserve(2*N); int a, b, c; for (int i = 0; i < N; ++i) { std::cin >> a >> b >> c; Tasks.emplace_back(a, b, c); } } void InitializeContainers() { for (int i = 0; i < Tasks.size(); ++i) { auto& task = Tasks[i]; TaskEvents.emplace_back(i, EvntType::Start, task.startTime); TaskEvents.emplace_back(i, EvntType::Finish, task.finishTime); if (task.finishTime - task.startTime - task.execTime < 0) throw std::runtime_error("Wrong input data!"); if (task.finishTime - task.startTime - task.execTime != 0) { Overlaps.emplace_back(task.startTime, task.finishTime, task.finishTime - task.startTime - task.execTime); } } std::sort(TaskEvents.begin(), TaskEvents.end()); std::sort(Overlaps.begin(), Overlaps.end(), [](const Overlap& lhs, const Overlap& rhs) { return lhs.startTime < rhs.startTime; }); int taskAmount{ 1 }; for (int i = 1; i < TaskEvents.size(); ++i) { const auto& evnt = TaskEvents[i]; if (TaskEvents[i - 1].value != evnt.value) { TaskGroups.emplace_back(TaskEvents[i - 1].value, evnt.value, taskAmount); } if (evnt.type == EvntType::Start) { taskAmount++; } else if (evnt.type == EvntType::Finish) { taskAmount--; } } } void SliceGroup(TaskGroup& group, int slicePt) { if ( slicePt >= group.finishTime || slicePt <= group.startTime) throw std::runtime_error("Wrong group slicing!"); TaskGroups.emplace_front( slicePt, group.finishTime, group.amount ); group.finishTime = slicePt; } bool Analize() { while (!TaskGroups.empty()) { auto group = TaskGroups.front(); TaskGroups.pop_front(); while ( !Overlaps.empty() && Overlaps[0].startTime <= group.startTime) { if (Overlaps[0].finishTime > group.startTime) OverlapQueue.push(Overlaps[0]); Overlaps.pop_front(); } while (group.amount > M) { if (OverlapQueue.empty()) return false; Overlap overlap = OverlapQueue.top(); OverlapQueue.pop(); if (overlap.finishTime <= group.startTime) continue; if (!OverlapQueue.empty() && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) < group.finishTime && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) > group.startTime && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) > overlap.finishTime - overlap.overlap && (OverlapQueue.top().finishTime - OverlapQueue.top().overlap) < overlap.finishTime) { int slicePt = OverlapQueue.top().finishTime - OverlapQueue.top().overlap; int newOv = slicePt - (overlap.finishTime - overlap.overlap); if (overlap.overlap - newOv <= 0) throw std::runtime_error("Somethings wrong in overlaps slicing!"); Overlaps.emplace_front(slicePt, overlap.finishTime, overlap.overlap - newOv); overlap.finishTime = slicePt; overlap.overlap = newOv; SliceGroup(group, slicePt); if (overlap.finishTime <= group.startTime) { throw std::runtime_error("Wrong after overlap slicing"); } } int preprevOv = overlap.overlap; overlap.overlap = std::min(overlap.finishTime - std::max( overlap.startTime, group.startTime ), overlap.overlap); if (overlap.overlap == 0) throw std::runtime_error("Somethings wrong in group slicing after using full overlap."); int prevOv = overlap.overlap; overlap.overlap -= (group.finishTime - group.startTime); if (overlap.overlap > 0) { overlap.startTime = group.finishTime; Overlaps.push_front(overlap); } else if (overlap.overlap < 0) { SliceGroup(group, group.finishTime + overlap.overlap); } --(group.amount); } } return true; } int main() { try { GetInput(); InitializeContainers(); if (Analize()) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } return 0; } |