#include <iostream> #include <queue> #include <algorithm> #include <vector> using namespace std; struct Job { int p, k, c; bool operator<(const Job& rhs) const { return make_pair(k, p) < make_pair(rhs.k, rhs.p); } }; istream& operator>>(istream& is, Job& job) { return is >> job.p >> job.k >> job.c; } ostream& operator<<(ostream& os, const Job& job) { return os << job.p << ", " << job.k << ", " << job.c; } typedef priority_queue<int, vector<int>, greater<int> > pqi; inline void start(const Job& job, pqi& finishes, int current_time) { int start_time = max(current_time, job.p); int end_time = start_time + job.c; // cout << "Inserting job " << job << ": will run " << start_time << " to " << end_time << endl; finishes.push(end_time); } int main() { cin.sync_with_stdio(false); int n, m; cin >> n >> m; // jobs, cpus vector<Job> jobs(n); for (int i=0; i<n; ++i) cin >> jobs[i]; sort(jobs.begin(), jobs.end()); int current_time = 0; pqi finishes; for (int i=0; i<n; ++i) { // cout << jobs[i] << endl; if (finishes.size() < m) { start(jobs[i], finishes, current_time); continue; } current_time = finishes.top(); // cout << "Time advances to " << current_time << endl; while (!finishes.empty() && finishes.top() == current_time) finishes.pop(); if (current_time + jobs[i].c > jobs[i].k) { cout << "NIE" << endl; return 0; } start(jobs[i], finishes, current_time); } cout << "TAK" << 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 | #include <iostream> #include <queue> #include <algorithm> #include <vector> using namespace std; struct Job { int p, k, c; bool operator<(const Job& rhs) const { return make_pair(k, p) < make_pair(rhs.k, rhs.p); } }; istream& operator>>(istream& is, Job& job) { return is >> job.p >> job.k >> job.c; } ostream& operator<<(ostream& os, const Job& job) { return os << job.p << ", " << job.k << ", " << job.c; } typedef priority_queue<int, vector<int>, greater<int> > pqi; inline void start(const Job& job, pqi& finishes, int current_time) { int start_time = max(current_time, job.p); int end_time = start_time + job.c; // cout << "Inserting job " << job << ": will run " << start_time << " to " << end_time << endl; finishes.push(end_time); } int main() { cin.sync_with_stdio(false); int n, m; cin >> n >> m; // jobs, cpus vector<Job> jobs(n); for (int i=0; i<n; ++i) cin >> jobs[i]; sort(jobs.begin(), jobs.end()); int current_time = 0; pqi finishes; for (int i=0; i<n; ++i) { // cout << jobs[i] << endl; if (finishes.size() < m) { start(jobs[i], finishes, current_time); continue; } current_time = finishes.top(); // cout << "Time advances to " << current_time << endl; while (!finishes.empty() && finishes.top() == current_time) finishes.pop(); if (current_time + jobs[i].c > jobs[i].k) { cout << "NIE" << endl; return 0; } start(jobs[i], finishes, current_time); } cout << "TAK" << endl; return 0; } |