Untitled
unknown
plain_text
16 days ago
3.3 kB
3
Indexable
Never
#include<iostream> #include<set> #include<unordered_map> using namespace std; #define MAXN 70005 int baseT, baseF, unitT, unitF; int cap, num; struct Car{ int id, timeDen, timeDi, timeDoi; bool isParking; }cars[MAXN]; struct cmp{ bool operator()(Car a, Car b){ int x = a.timeDoi - a.timeDi -a.timeDen; int y = b.timeDoi - b.timeDi -b.timeDen; if(x == y) return a.timeDen < b.timeDen; return x > y; } }; unordered_map<int, int> myMap; set<Car, cmp> waitlist; void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) { baseT = mBaseTime; baseF = mBaseFee; unitT = mUnitTime; unitF = mUnitFee; cap = mCapacity; num = 0; myMap.clear(); waitlist.clear(); } int arrive(int mTime, int mCar) { //TH1: xe moi den chua tung do o bai do xe if(myMap.find(mCar) == myMap.end()){ //neu con cho do xe if(cap){ myMap[mCar] = num; cars[num].id = mCar; cars[num].timeDi = 0; cars[num].timeDoi = 0; cars[num].timeDen = mTime; cars[num].isParking = true; num++; cap--; } else{ myMap[mCar] = num; cars[num].id = mCar; cars[num].timeDi = 0; cars[num].timeDoi = 0; cars[num].timeDen = mTime; cars[num].isParking = false; waitlist.insert(cars[num]); num++; } } //TH2: xe tung do o bai do xe -> cap nhat gia tri moi cho xe else{ if(cap){ cars[myMap[mCar]].isParking = true; cars[myMap[mCar]].timeDen = mTime; cap--; } else{ cars[myMap[mCar]].isParking = false; cars[myMap[mCar]].timeDen = mTime; waitlist.insert(cars[myMap[mCar]]); } } return waitlist.size(); } int leave(int mTime, int mCar) { int idCar = myMap[mCar]; Car currCar = cars[myMap[mCar]]; // roi di ma chua park if(waitlist.find(currCar) != waitlist.end()){ waitlist.erase(currCar); cars[idCar].timeDoi += mTime - currCar.timeDen; return -1; } cars[idCar].isParking = false; cars[idCar].timeDi += mTime - cars[idCar].timeDen; int parkTime_thisTurn = mTime - cars[idCar].timeDen; int timeN = (parkTime_thisTurn - baseT) / unitT; int timeMod = (parkTime_thisTurn - baseT) % unitT; if(timeN < 0) timeN = 0; else if(timeMod>0) timeN++; int ans = baseF + timeN * unitF; //neu sau khi xe ra thi kh con xe nao dang doi nua -> bai do xe du 1 cho trong if(!waitlist.size()){ cars[idCar].timeDen = mTime; cap++; } //van con xe dang doi -> dua xe tiep theo vao bai do xe else{ auto topCar = *waitlist.begin(); int idx = topCar.id; auto idNextCar = myMap[idx]; cars[idNextCar].isParking = true; cars[idNextCar].timeDoi += (mTime - cars[idNextCar].timeDen); cars[idNextCar].timeDen = mTime; waitlist.erase(waitlist.begin()); } return ans; }
Leave a Comment