#include <bits/stdc++.h> using namespace std; pair <int, int> process[105]; int Tprocess[105]; bool running[105]; pair <int, int> process1[105]; int Tprocess1[105]; bool running1[105]; int qime = -1; bool sortuj(int ID1, int ID2) { bool a = true, b = true ,c = true; if(process[ID1].second - qime == Tprocess[ID1]) return a; if(process[ID2].second - qime == Tprocess[ID2]) return !a; if(Tprocess[ID1] > Tprocess[ID2]) return b; if(Tprocess[ID1] < Tprocess[ID2]) return !b; if(process[ID1].second > process[ID2].second) return c; if(process[ID1].second < process[ID2].second) return !c; } bool sortujs(int ID1, int ID2) { bool a = false,b = false ,c = true; if(process[ID1].second - Tprocess[ID1] > process[ID2].second - Tprocess[ID2]) return a; if(process[ID1].second - Tprocess[ID1] < process[ID2].second - Tprocess[ID2]) return !a; if(Tprocess[ID1] > Tprocess[ID2]) return b; if(Tprocess[ID1] < Tprocess[ID2]) return !b; if(process[ID1].second > process[ID2].second) return c; if(process[ID1].second < process[ID2].second) return !c; } int getnextmin(int query, int base) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min && process[i].first > base) min = process[i].first; } return min; } int getendproc(int query) // returns end qime of last processor { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].second < min && running[i]) min = process[i].second; } return min; } int getlowproc(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(Tprocess[i] < min && running[i]) min = Tprocess[i]; } return min; } int getanyproc(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(Tprocess[i] < min) min = Tprocess[i]; } return min; } int getnextT(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min) min = process[i].first; } return min; } int getnextTime(int query, int buff) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min && process[i].first > buff) min = process[i].first; } return min; } bool run(int query, int qime, int proc , bool fasts) { bool rozl = false, savet = false; while(1) { if(savet == false) { qime = getnextTime(query, qime); // printf("pulling new time -> "); } else savet = false; // printf("time %d ", qime); for(int i = 0 ; i < 105; i++) if(Tprocess[i] == 0) { process[i].first = 1100000000; process[i].second =1100000000; } for(int i = 0 ; i < 105; i++) if(process[i].first <= qime && process[i].second > qime && Tprocess[i] > 0) { running[i] = true; // printf("adding job %d\n", i); } else running[i]= false; vector <int> processq; for(int i = 0 ; i < 105; i++) if(running[i]) processq.push_back(i); if(!fasts) sort (processq.begin(), processq.end(), sortuj); else sort (processq.begin(), processq.end(), sortujs); // printf("running %d processes: {", processq.size()); // for(int i = 0 ; i < processq.size(); i++) // printf("(P %d T %d )", processq[i], Tprocess[processq[i]]); // printf("}"); int workqime = min(getnextmin(query, qime)-qime, getlowproc(query)); // minimum z czasu do następnego gościa, i pracy z moich procesów workqime = min(workqime, getendproc(query)-qime); // minimum do tego z końca pracy z jednego z moich gości // printf("ANALIZA %d %d %d %d -> ", workqime, getnextmin(query, qime)-qime, getlowproc(query), getendproc(query)-qime); // printf(" runqime = %d\n", workqime); int numba = processq.size(); int runningt = min(proc, numba); if(!fasts) workqime = 1; for(int i = 0 ; i < runningt; i++) { // printf("running process %d for %d left %d\n", processq[i], workqime, Tprocess[processq[i]]-workqime); Tprocess[processq[i]] -= workqime; } for(int i = 0 ; i < query; i++) if(running[i]) process[i].first += workqime; int jobs = 0; for(int i = 0 ; i < 105; i++) jobs += Tprocess[i]; if(workqime == 0) { return false; } for(int i = 0 ; i < 105; i++) if(process[i].first >= 1100000000 && process[i].second >= 1100000000 && Tprocess[i] > 0) { return false; } if(getlowproc(query) == 1100000000 && rozl == false) { // printf("%d -> ", qime); qime = getnextTime(query, qime); // printf("%d\n", qime); // printf("corrected"); rozl = true; savet = true; } else { if(getlowproc(query) == 1100000000 && rozl == true) { qime = 0; return false; rozl = true; } else rozl = false; } // printf("JOBS = %d\n", jobs); if(jobs == 0) break; } // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); // printf("TAK"); return true; } int main() { int query, proc; scanf("%d %d", &query, &proc); int maxim=0; for(int i = 0 ; i < query; i++) { // printf("read %d\n", i); int p, k, s; scanf("%d %d %d", &p, &k, &s); if(maxim < k) maxim = k; process[i].first = p; process[i].second =k; Tprocess[i] = s; process1[i].first = p; process1[i].second =k; Tprocess1[i] = s; } bool state1 = run(query, qime, proc, true); qime = -1; // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); //printf("--------------------------------->\n"); if(state1) {printf("TAK"); return 0;} for(int i = 0 ; i < 105; i++) { process[i].first = process1[i].first; process[i].second = process1[i].second; Tprocess[i] = Tprocess1[i]; } if(maxim < 100000) { bool state2 = run(query, qime, proc, false); // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); if(state2) printf("TAK"); else printf("NIE"); return 0; } else printf("NIE"); }
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 | #include <bits/stdc++.h> using namespace std; pair <int, int> process[105]; int Tprocess[105]; bool running[105]; pair <int, int> process1[105]; int Tprocess1[105]; bool running1[105]; int qime = -1; bool sortuj(int ID1, int ID2) { bool a = true, b = true ,c = true; if(process[ID1].second - qime == Tprocess[ID1]) return a; if(process[ID2].second - qime == Tprocess[ID2]) return !a; if(Tprocess[ID1] > Tprocess[ID2]) return b; if(Tprocess[ID1] < Tprocess[ID2]) return !b; if(process[ID1].second > process[ID2].second) return c; if(process[ID1].second < process[ID2].second) return !c; } bool sortujs(int ID1, int ID2) { bool a = false,b = false ,c = true; if(process[ID1].second - Tprocess[ID1] > process[ID2].second - Tprocess[ID2]) return a; if(process[ID1].second - Tprocess[ID1] < process[ID2].second - Tprocess[ID2]) return !a; if(Tprocess[ID1] > Tprocess[ID2]) return b; if(Tprocess[ID1] < Tprocess[ID2]) return !b; if(process[ID1].second > process[ID2].second) return c; if(process[ID1].second < process[ID2].second) return !c; } int getnextmin(int query, int base) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min && process[i].first > base) min = process[i].first; } return min; } int getendproc(int query) // returns end qime of last processor { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].second < min && running[i]) min = process[i].second; } return min; } int getlowproc(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(Tprocess[i] < min && running[i]) min = Tprocess[i]; } return min; } int getanyproc(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(Tprocess[i] < min) min = Tprocess[i]; } return min; } int getnextT(int query) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min) min = process[i].first; } return min; } int getnextTime(int query, int buff) { int min = 1100000000; for(int i = 0 ; i < query; i++) { if(process[i].first < min && process[i].first > buff) min = process[i].first; } return min; } bool run(int query, int qime, int proc , bool fasts) { bool rozl = false, savet = false; while(1) { if(savet == false) { qime = getnextTime(query, qime); // printf("pulling new time -> "); } else savet = false; // printf("time %d ", qime); for(int i = 0 ; i < 105; i++) if(Tprocess[i] == 0) { process[i].first = 1100000000; process[i].second =1100000000; } for(int i = 0 ; i < 105; i++) if(process[i].first <= qime && process[i].second > qime && Tprocess[i] > 0) { running[i] = true; // printf("adding job %d\n", i); } else running[i]= false; vector <int> processq; for(int i = 0 ; i < 105; i++) if(running[i]) processq.push_back(i); if(!fasts) sort (processq.begin(), processq.end(), sortuj); else sort (processq.begin(), processq.end(), sortujs); // printf("running %d processes: {", processq.size()); // for(int i = 0 ; i < processq.size(); i++) // printf("(P %d T %d )", processq[i], Tprocess[processq[i]]); // printf("}"); int workqime = min(getnextmin(query, qime)-qime, getlowproc(query)); // minimum z czasu do następnego gościa, i pracy z moich procesów workqime = min(workqime, getendproc(query)-qime); // minimum do tego z końca pracy z jednego z moich gości // printf("ANALIZA %d %d %d %d -> ", workqime, getnextmin(query, qime)-qime, getlowproc(query), getendproc(query)-qime); // printf(" runqime = %d\n", workqime); int numba = processq.size(); int runningt = min(proc, numba); if(!fasts) workqime = 1; for(int i = 0 ; i < runningt; i++) { // printf("running process %d for %d left %d\n", processq[i], workqime, Tprocess[processq[i]]-workqime); Tprocess[processq[i]] -= workqime; } for(int i = 0 ; i < query; i++) if(running[i]) process[i].first += workqime; int jobs = 0; for(int i = 0 ; i < 105; i++) jobs += Tprocess[i]; if(workqime == 0) { return false; } for(int i = 0 ; i < 105; i++) if(process[i].first >= 1100000000 && process[i].second >= 1100000000 && Tprocess[i] > 0) { return false; } if(getlowproc(query) == 1100000000 && rozl == false) { // printf("%d -> ", qime); qime = getnextTime(query, qime); // printf("%d\n", qime); // printf("corrected"); rozl = true; savet = true; } else { if(getlowproc(query) == 1100000000 && rozl == true) { qime = 0; return false; rozl = true; } else rozl = false; } // printf("JOBS = %d\n", jobs); if(jobs == 0) break; } // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); // printf("TAK"); return true; } int main() { int query, proc; scanf("%d %d", &query, &proc); int maxim=0; for(int i = 0 ; i < query; i++) { // printf("read %d\n", i); int p, k, s; scanf("%d %d %d", &p, &k, &s); if(maxim < k) maxim = k; process[i].first = p; process[i].second =k; Tprocess[i] = s; process1[i].first = p; process1[i].second =k; Tprocess1[i] = s; } bool state1 = run(query, qime, proc, true); qime = -1; // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); //printf("--------------------------------->\n"); if(state1) {printf("TAK"); return 0;} for(int i = 0 ; i < 105; i++) { process[i].first = process1[i].first; process[i].second = process1[i].second; Tprocess[i] = Tprocess1[i]; } if(maxim < 100000) { bool state2 = run(query, qime, proc, false); // for(int i = 0 ; i < query; i++) // printf("%d %d %d %d\n", i, process[i].first, process[i].second, Tprocess[i]); if(state2) printf("TAK"); else printf("NIE"); return 0; } else printf("NIE"); } |