Untitled
unknown
plain_text
7 days ago
3.3 kB
2
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, tDen, totalTimeParking, timeDoi; bool isParking; }cars[MAXN]; struct cmp{ bool operator()(Car a, Car b){ int x = a.timeDoi - a.totalTimeParking -a.tDen; int y = b.timeDoi - b.totalTimeParking -b.tDen; if(x == y) return a.tDen < b.tDen; 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].totalTimeParking = 0; cars[num].timeDoi = 0; cars[num].tDen = mTime; cars[num].isParking = true; num++; cap--; } else{ myMap[mCar] = num; cars[num].id = mCar; cars[num].totalTimeParking = 0; cars[num].timeDoi = 0; cars[num].tDen = 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]].tDen = mTime; cap--; } else{ cars[myMap[mCar]].isParking = false; cars[myMap[mCar]].tDen = 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.tDen; return -1; } cars[idCar].isParking = false; cars[idCar].totalTimeParking += mTime - cars[idCar].tDen; int timeParking = mTime - cars[idCar].tDen; int timeN = (timeParking - baseT) / unitT; int timeMod = (timeParking - 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()){ 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].tDen); cars[idNextCar].tDen = mTime; waitlist.erase(waitlist.begin()); } return ans; }
Leave a Comment