// to jest heura, ale może Bajtazar się nie skapnie #include <iostream> #include <algorithm> #include <vector> #include <set> #include <queue> using namespace std; bool enableHeur1=false; struct Task{ int p,k,c; bool operator<(const Task& t)const{ // :) if(enableHeur1){ int r1=(k-p)-c; int r2=(t.k-t.p)-t.c; if (r1<r2) return 1; if (r1>r2) return 0; } if(p==t.p){ if(k==t.k) return c<t.c; else return k<t.k; }else return p<t.p; } bool operator>(const Task& t)const{ return !(*this<t); } }; int n,m; const int MAXN=100; int maxk=-1; // temporary vectors Task tasks[MAXN]; vector<Task> usedNow; vector<Task> temp; bool try1(){ enableHeur1=false; sort(tasks, tasks+n); enableHeur1=true; //cout<<"starting tasks:"<<endl; //cout<<tasks[0].p<<" "<<tasks[0].k<<" c="<<tasks[0].c<<endl; //cout<<tasks[1].p<<" "<<tasks[1].k<<" c="<<tasks[1].c<<endl; //cout<<tasks[2].p<<" "<<tasks[2].k<<" c="<<tasks[2].c<<endl; //cout<<(tasks[0]>tasks[2])<<endl; //cout<<(tasks[0]>tasks[2])<<endl; //set<Task> curTasks; priority_queue<Task,vector<Task>,greater<Task> > curTasks; int i=1; int p=tasks[0].p; //cout<<"starting tasks:"<<endl; //cout<<tasks[0].p<<" "<<tasks[0].k<<" c="<<tasks[0].c<<endl; curTasks.push(tasks[0]); while(i<n && (p == tasks[i].p)){ //cout<<tasks[i].p<<" "<<tasks[i].k<<" c="<<tasks[i].c<<endl; curTasks.push(tasks[i]); ++i; } bool lastIteration=false; while(i<=n){ int newP; if(i<n){ newP=tasks[i].p; }else{ newP=maxk; //TODO } const int dt=newP-p; //cout<<"-----------------------------------------"<<endl; //cout<<"i="<<i<<", "; //cout<<"last p="<<p<<", newP="<<newP<<", "; //cout<<"dt="<<dt<<", "; //cout<<"size="<<curTasks.size()<<endl; for(int a=0;a<dt;++a){ usedNow.clear(); for(int b=0;b<m;++b){ if(curTasks.empty()) break; Task popped=curTasks.top(); curTasks.pop(); //cout<<"popped.k="<<popped.k<<endl; if(p+a >= popped.k){ return 0; } --popped.c; usedNow.push_back(popped); } for(auto b:usedNow){ if(b.c>0) curTasks.push(b); } } temp.clear(); // rewrite tasks with current p while(!curTasks.empty()){ temp.push_back(curTasks.top()); curTasks.pop(); } // add new tasks while(i<n && (newP == tasks[i].p)){ temp.push_back(tasks[i]); ++i; } // update new tasks //cout<<"new tasks:"<<endl; for(auto a:temp){ Task tem=a; if(tem.c>0){ tem.p=newP; curTasks.push(tem); //cout<<tem.p<<" "<<tem.k<<" c="<<tem.c<<endl; if(tem.c > tem.k-tem.p){ return 0; } } } if(lastIteration) break; lastIteration=(i==n); p=newP; } return 1; } int main(){ ios_base::sync_with_stdio(0); cin>>n>>m; for(int a=0;a<n;++a){ cin>>tasks[a].p>>tasks[a].k>>tasks[a].c; maxk=max(maxk,tasks[a].k); } maxk+=2; bool b=try1(); if(b){ cout<<"TAK\n"; return 0; } for(int a=0;a<n;++a){ tasks[a].p=maxk-tasks[a].p; tasks[a].k=maxk-tasks[a].k; swap(tasks[a].p, tasks[a].k); } maxk+=5; b=try1(); if(b){ cout<<"TAK\n"; return 0; } cout<<"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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | // to jest heura, ale może Bajtazar się nie skapnie #include <iostream> #include <algorithm> #include <vector> #include <set> #include <queue> using namespace std; bool enableHeur1=false; struct Task{ int p,k,c; bool operator<(const Task& t)const{ // :) if(enableHeur1){ int r1=(k-p)-c; int r2=(t.k-t.p)-t.c; if (r1<r2) return 1; if (r1>r2) return 0; } if(p==t.p){ if(k==t.k) return c<t.c; else return k<t.k; }else return p<t.p; } bool operator>(const Task& t)const{ return !(*this<t); } }; int n,m; const int MAXN=100; int maxk=-1; // temporary vectors Task tasks[MAXN]; vector<Task> usedNow; vector<Task> temp; bool try1(){ enableHeur1=false; sort(tasks, tasks+n); enableHeur1=true; //cout<<"starting tasks:"<<endl; //cout<<tasks[0].p<<" "<<tasks[0].k<<" c="<<tasks[0].c<<endl; //cout<<tasks[1].p<<" "<<tasks[1].k<<" c="<<tasks[1].c<<endl; //cout<<tasks[2].p<<" "<<tasks[2].k<<" c="<<tasks[2].c<<endl; //cout<<(tasks[0]>tasks[2])<<endl; //cout<<(tasks[0]>tasks[2])<<endl; //set<Task> curTasks; priority_queue<Task,vector<Task>,greater<Task> > curTasks; int i=1; int p=tasks[0].p; //cout<<"starting tasks:"<<endl; //cout<<tasks[0].p<<" "<<tasks[0].k<<" c="<<tasks[0].c<<endl; curTasks.push(tasks[0]); while(i<n && (p == tasks[i].p)){ //cout<<tasks[i].p<<" "<<tasks[i].k<<" c="<<tasks[i].c<<endl; curTasks.push(tasks[i]); ++i; } bool lastIteration=false; while(i<=n){ int newP; if(i<n){ newP=tasks[i].p; }else{ newP=maxk; //TODO } const int dt=newP-p; //cout<<"-----------------------------------------"<<endl; //cout<<"i="<<i<<", "; //cout<<"last p="<<p<<", newP="<<newP<<", "; //cout<<"dt="<<dt<<", "; //cout<<"size="<<curTasks.size()<<endl; for(int a=0;a<dt;++a){ usedNow.clear(); for(int b=0;b<m;++b){ if(curTasks.empty()) break; Task popped=curTasks.top(); curTasks.pop(); //cout<<"popped.k="<<popped.k<<endl; if(p+a >= popped.k){ return 0; } --popped.c; usedNow.push_back(popped); } for(auto b:usedNow){ if(b.c>0) curTasks.push(b); } } temp.clear(); // rewrite tasks with current p while(!curTasks.empty()){ temp.push_back(curTasks.top()); curTasks.pop(); } // add new tasks while(i<n && (newP == tasks[i].p)){ temp.push_back(tasks[i]); ++i; } // update new tasks //cout<<"new tasks:"<<endl; for(auto a:temp){ Task tem=a; if(tem.c>0){ tem.p=newP; curTasks.push(tem); //cout<<tem.p<<" "<<tem.k<<" c="<<tem.c<<endl; if(tem.c > tem.k-tem.p){ return 0; } } } if(lastIteration) break; lastIteration=(i==n); p=newP; } return 1; } int main(){ ios_base::sync_with_stdio(0); cin>>n>>m; for(int a=0;a<n;++a){ cin>>tasks[a].p>>tasks[a].k>>tasks[a].c; maxk=max(maxk,tasks[a].k); } maxk+=2; bool b=try1(); if(b){ cout<<"TAK\n"; return 0; } for(int a=0;a<n;++a){ tasks[a].p=maxk-tasks[a].p; tasks[a].k=maxk-tasks[a].k; swap(tasks[a].p, tasks[a].k); } maxk+=5; b=try1(); if(b){ cout<<"TAK\n"; return 0; } cout<<"NIE\n"; return 0; } |