#include <queue> #include <algorithm> #include <iostream> using namespace std; struct Task { int p; int k; int c_left; int started; int prio; }; struct Comparator { bool operator() (const Task& lhs, const Task& rhs) { return lhs.prio > rhs.prio; } }; struct Comparator_ends { bool operator() (const Task& lhs, const Task& rhs) { return lhs.started + lhs.c_left > rhs.started + rhs.c_left; } }; bool compareByP(const Task& lhs, const Task& rhs) { return lhs.p < rhs.p; } priority_queue<Task, vector<Task>, Comparator> que[100]; vector<Task> tasks; vector<int> starts; void fillQueues() { int start = -1; for(unsigned int i=0; i<tasks.size(); ++i) { if (tasks[i].p != start) { starts.push_back(tasks[i].p); start = tasks[i].p; } que[starts.size()-1].push(tasks[i]); } } bool schedule(int m) { priority_queue<Task, vector<Task>, Comparator_ends> scheduled; for (unsigned int i=0; i<starts.size(); ++i) { while ((scheduled.size() < m) and (que[i].size() > 0)) { auto task = que[i].top(); if (task.prio < 0) return false; task.started = starts[i]; scheduled.push(task); que[i].pop(); } if (i < starts.size()-1) { while ((scheduled.top().started + scheduled.top().c_left < starts[i+1]) and (que[i].size() > 0)) { int started = scheduled.top().started + scheduled.top().c_left; scheduled.pop(); auto task = que[i].top(); task.started = started; if (task.started + task.c_left > task.k) return false; scheduled.push(task); que[i].pop(); } while (que[i].size() > 0) { auto task = que[i].top(); task.prio = task.k - task.c_left - starts[i+1]; que[i+1].push(task); que[i].pop(); } while (scheduled.size() > 0) { auto task = scheduled.top(); task.c_left -= (starts[i+1] - task.started); if(task.c_left > 0) { task.prio = task.k - task.c_left - starts[i+1]; que[i+1].push(task); } scheduled.pop(); } } else { while (que[i].size() > 0) { int started = scheduled.top().started + scheduled.top().c_left; scheduled.pop(); auto task = que[i].top(); task.started = started; if (task.started + task.c_left > task.k) return false; scheduled.push(task); que[i].pop(); } } } return true; } int main() { unsigned int n; unsigned int m; int p, k, c; cin >> n; cin >> m; for (unsigned int i=0; i<n; ++i) { Task t; cin >> t.p; cin >> t.k; cin >> t.c_left; t.prio = t.k - t.p - t.c_left; tasks.push_back(t); } sort(tasks.begin(), tasks.end(), compareByP); fillQueues(); auto res = schedule(m); cout << (res ? "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 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 | #include <queue> #include <algorithm> #include <iostream> using namespace std; struct Task { int p; int k; int c_left; int started; int prio; }; struct Comparator { bool operator() (const Task& lhs, const Task& rhs) { return lhs.prio > rhs.prio; } }; struct Comparator_ends { bool operator() (const Task& lhs, const Task& rhs) { return lhs.started + lhs.c_left > rhs.started + rhs.c_left; } }; bool compareByP(const Task& lhs, const Task& rhs) { return lhs.p < rhs.p; } priority_queue<Task, vector<Task>, Comparator> que[100]; vector<Task> tasks; vector<int> starts; void fillQueues() { int start = -1; for(unsigned int i=0; i<tasks.size(); ++i) { if (tasks[i].p != start) { starts.push_back(tasks[i].p); start = tasks[i].p; } que[starts.size()-1].push(tasks[i]); } } bool schedule(int m) { priority_queue<Task, vector<Task>, Comparator_ends> scheduled; for (unsigned int i=0; i<starts.size(); ++i) { while ((scheduled.size() < m) and (que[i].size() > 0)) { auto task = que[i].top(); if (task.prio < 0) return false; task.started = starts[i]; scheduled.push(task); que[i].pop(); } if (i < starts.size()-1) { while ((scheduled.top().started + scheduled.top().c_left < starts[i+1]) and (que[i].size() > 0)) { int started = scheduled.top().started + scheduled.top().c_left; scheduled.pop(); auto task = que[i].top(); task.started = started; if (task.started + task.c_left > task.k) return false; scheduled.push(task); que[i].pop(); } while (que[i].size() > 0) { auto task = que[i].top(); task.prio = task.k - task.c_left - starts[i+1]; que[i+1].push(task); que[i].pop(); } while (scheduled.size() > 0) { auto task = scheduled.top(); task.c_left -= (starts[i+1] - task.started); if(task.c_left > 0) { task.prio = task.k - task.c_left - starts[i+1]; que[i+1].push(task); } scheduled.pop(); } } else { while (que[i].size() > 0) { int started = scheduled.top().started + scheduled.top().c_left; scheduled.pop(); auto task = que[i].top(); task.started = started; if (task.started + task.c_left > task.k) return false; scheduled.push(task); que[i].pop(); } } } return true; } int main() { unsigned int n; unsigned int m; int p, k, c; cin >> n; cin >> m; for (unsigned int i=0; i<n; ++i) { Task t; cin >> t.p; cin >> t.k; cin >> t.c_left; t.prio = t.k - t.p - t.c_left; tasks.push_back(t); } sort(tasks.begin(), tasks.end(), compareByP); fillQueues(); auto res = schedule(m); cout << (res ? "TAK" : "NIE"); return 0; } |