#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; long totalProcTime = 0L; long min_start = 1000000L; long max_end = 0L; class Task { public: long start_time; long remaining_time; long spare_time; Task() : remaining_time(0L), start_time(0L), spare_time(0L) {} Task(long start, long end, long runtime) : remaining_time(runtime), start_time(start), spare_time(end - start - runtime) { totalProcTime += runtime; if(min_start > start) min_start = start; if(max_end < end) max_end = end; } bool operator< (const Task& rhc) const { if(start_time < rhc.start_time) return true; else if(start_time == rhc.start_time) { if(spare_time < rhc.spare_time) return true; else if(spare_time == rhc.spare_time) { if(remaining_time < rhc.remaining_time) return true; } } return false; } }; vector<Task> vecRunningTasks; long processors = 0L; void PopTime(long nTime) { long nTask = 0L; for(vector<Task>::iterator elem = vecRunningTasks.begin(); elem != vecRunningTasks.end();) { if(elem->start_time > 0) { elem->start_time -= nTime; ++elem; } else { if(nTask < processors) { elem->remaining_time -= nTime; if(elem->remaining_time == 0L) elem = vecRunningTasks.erase(elem); else ++elem; } else { elem->spare_time -= nTime; ++elem; } } ++nTask; } } long FindMinTime() { long local_min = 1000000L; if(vecRunningTasks[processors].start_time == 0L && vecRunningTasks[processors].spare_time == 0L) return -1L; vector<Task>::const_iterator elem = vecRunningTasks.begin(); vector<Task>::const_iterator sentinel = vecRunningTasks.begin() + processors; for(; elem != sentinel; ++elem) { if(elem->start_time > 0L) { if(elem->start_time < local_min) local_min = elem->start_time; } else { if(elem->remaining_time < local_min) local_min = elem->remaining_time; } } for(elem = sentinel; elem != vecRunningTasks.end(); ++elem) { if(elem->start_time > 0L) { if(elem->start_time < local_min) local_min = elem->start_time; } else if(elem->spare_time < local_min) local_min = elem->spare_time; } return local_min; } bool Process() { long nextTimeStep = 0L; while((long)vecRunningTasks.size() > processors) { sort(vecRunningTasks.begin(), vecRunningTasks.end()); nextTimeStep = FindMinTime(); if(nextTimeStep < 0L) return false; PopTime(nextTimeStep); } return true; } int main(int argc, char* argv[]) { long start_time = 0L; long end_time = 0L; long proc_time = 0L; long tasks = 0L; scanf("%ld %ld\n", &tasks, &processors); if(processors >= tasks) printf("TAK\n"); else { vecRunningTasks.reserve(tasks); while(tasks--) { scanf("%ld %ld %ld\n", &start_time, &end_time, &proc_time); vecRunningTasks.push_back(Task(start_time, end_time, proc_time)); } if(totalProcTime > (max_end - min_start) * processors) printf("NIE\n"); else if(Process()) printf("TAK\n"); else printf("NIE\n"); } 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 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; long totalProcTime = 0L; long min_start = 1000000L; long max_end = 0L; class Task { public: long start_time; long remaining_time; long spare_time; Task() : remaining_time(0L), start_time(0L), spare_time(0L) {} Task(long start, long end, long runtime) : remaining_time(runtime), start_time(start), spare_time(end - start - runtime) { totalProcTime += runtime; if(min_start > start) min_start = start; if(max_end < end) max_end = end; } bool operator< (const Task& rhc) const { if(start_time < rhc.start_time) return true; else if(start_time == rhc.start_time) { if(spare_time < rhc.spare_time) return true; else if(spare_time == rhc.spare_time) { if(remaining_time < rhc.remaining_time) return true; } } return false; } }; vector<Task> vecRunningTasks; long processors = 0L; void PopTime(long nTime) { long nTask = 0L; for(vector<Task>::iterator elem = vecRunningTasks.begin(); elem != vecRunningTasks.end();) { if(elem->start_time > 0) { elem->start_time -= nTime; ++elem; } else { if(nTask < processors) { elem->remaining_time -= nTime; if(elem->remaining_time == 0L) elem = vecRunningTasks.erase(elem); else ++elem; } else { elem->spare_time -= nTime; ++elem; } } ++nTask; } } long FindMinTime() { long local_min = 1000000L; if(vecRunningTasks[processors].start_time == 0L && vecRunningTasks[processors].spare_time == 0L) return -1L; vector<Task>::const_iterator elem = vecRunningTasks.begin(); vector<Task>::const_iterator sentinel = vecRunningTasks.begin() + processors; for(; elem != sentinel; ++elem) { if(elem->start_time > 0L) { if(elem->start_time < local_min) local_min = elem->start_time; } else { if(elem->remaining_time < local_min) local_min = elem->remaining_time; } } for(elem = sentinel; elem != vecRunningTasks.end(); ++elem) { if(elem->start_time > 0L) { if(elem->start_time < local_min) local_min = elem->start_time; } else if(elem->spare_time < local_min) local_min = elem->spare_time; } return local_min; } bool Process() { long nextTimeStep = 0L; while((long)vecRunningTasks.size() > processors) { sort(vecRunningTasks.begin(), vecRunningTasks.end()); nextTimeStep = FindMinTime(); if(nextTimeStep < 0L) return false; PopTime(nextTimeStep); } return true; } int main(int argc, char* argv[]) { long start_time = 0L; long end_time = 0L; long proc_time = 0L; long tasks = 0L; scanf("%ld %ld\n", &tasks, &processors); if(processors >= tasks) printf("TAK\n"); else { vecRunningTasks.reserve(tasks); while(tasks--) { scanf("%ld %ld %ld\n", &start_time, &end_time, &proc_time); vecRunningTasks.push_back(Task(start_time, end_time, proc_time)); } if(totalProcTime > (max_end - min_start) * processors) printf("NIE\n"); else if(Process()) printf("TAK\n"); else printf("NIE\n"); } return 0; } |