#include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <algorithm> struct task_t { int begin, end, time; bool operator<(const task_t & other) const { return begin < other.begin; } }; struct realized_task_t { int remaining, deadline, id; bool operator<(const realized_task_t & other) const { if (remaining == other.remaining) return id < other.id; return remaining < other.remaining; } }; int main() { int n, m; scanf("%d %d", &n, &m); std::vector<task_t> tasks; tasks.reserve(n); int begin = 1000 * 1000, end = 0; for (int i = 0; i < n; i++) { task_t t; scanf("%d %d %d", &t.begin, &t.end, &t.time); begin = std::min(begin, t.begin); end = std::max(end, t.end); tasks.emplace_back(std::move(t)); } std::sort(tasks.begin(), tasks.end()); int used = 0; std::set<realized_task_t> running; std::vector<realized_task_t> runChoice; for (int i = begin; i < end; i++) { while (used < n && tasks[used].begin >= i) { running.insert({ tasks[used].time, tasks[used].end, used }); used++; } // Decide what to run in this iteration int count = 0; for (auto it = running.begin(); count < m && it != running.end(); ++count) { auto it2 = it; it++; runChoice.push_back(*it2); running.erase(it2); } for (auto & t : runChoice) { if (t.deadline <= i) { puts("NIE"); return 0; } t.remaining--; if (t.remaining > 0) { running.insert(t); } } runChoice.clear(); } puts(running.empty() ? "TAK" : "NIE"); 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <algorithm> struct task_t { int begin, end, time; bool operator<(const task_t & other) const { return begin < other.begin; } }; struct realized_task_t { int remaining, deadline, id; bool operator<(const realized_task_t & other) const { if (remaining == other.remaining) return id < other.id; return remaining < other.remaining; } }; int main() { int n, m; scanf("%d %d", &n, &m); std::vector<task_t> tasks; tasks.reserve(n); int begin = 1000 * 1000, end = 0; for (int i = 0; i < n; i++) { task_t t; scanf("%d %d %d", &t.begin, &t.end, &t.time); begin = std::min(begin, t.begin); end = std::max(end, t.end); tasks.emplace_back(std::move(t)); } std::sort(tasks.begin(), tasks.end()); int used = 0; std::set<realized_task_t> running; std::vector<realized_task_t> runChoice; for (int i = begin; i < end; i++) { while (used < n && tasks[used].begin >= i) { running.insert({ tasks[used].time, tasks[used].end, used }); used++; } // Decide what to run in this iteration int count = 0; for (auto it = running.begin(); count < m && it != running.end(); ++count) { auto it2 = it; it++; runChoice.push_back(*it2); running.erase(it2); } for (auto & t : runChoice) { if (t.deadline <= i) { puts("NIE"); return 0; } t.remaining--; if (t.remaining > 0) { running.insert(t); } } runChoice.clear(); } puts(running.empty() ? "TAK" : "NIE"); return 0; } |