Untitled
unknown
plain_text
a year ago
3.0 kB
13
Indexable
#include <unordered_map>
#include <set>
#include <cmath>
using namespace std;
struct Car{
int id;
int status;
int timeJoin;
int parkingTime;
int totalParkingTime;
int totalWaitingTime;
int val() {
return totalWaitingTime - totalParkingTime;
}
};
Car cars[70001];
struct cmp{
bool operator()(int a, int b) {
int val1 = cars[a].val();
int val2 = cars[b].val();
if(val1 == val2) return cars[a].timeJoin < cars[b].timeJoin;
return val1 > val2;
}
};
unordered_map<int, int> mp;
set<int, cmp> se;
unordered_map<int, int> history;
int baseTime, baseFee, unitTime, unitFee, capacity;
int cnt;
void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) {
se.clear();
mp.clear();
history.clear();
cnt = -1;
baseTime = mBaseTime;
baseFee = mBaseFee;
unitFee = mUnitFee;
unitTime = mUnitTime;
capacity = mCapacity;
return;
}
int arrive(int mTime, int mCar) {
if(history.find(mCar) == history.end()) { //Chua join parking bao gio
cnt++;
history[mCar] = cnt;
if(mp.size() < capacity) { //Con slot parking
mp[mCar] = mTime;
cars[history[mCar]].id = mCar;
cars[history[mCar]].status = 1; //1: into parking, 2: waiting
cars[history[mCar]].timeJoin = mTime;
cars[history[mCar]].parkingTime = 0;
cars[history[mCar]].totalParkingTime = 0;
cars[history[mCar]].totalWaitingTime = 0;
}
else { //Het slot parking
cars[history[mCar]].id = mCar;
cars[history[mCar]].status = 2;
cars[history[mCar]].timeJoin = mTime;
cars[history[mCar]].parkingTime = 0;
cars[history[mCar]].totalParkingTime = 0;
cars[history[mCar]].totalWaitingTime = 0;
se.insert(cnt);
}
}
else { // Da tung join parking
if(mp.size() < capacity) { //Con slot
mp[mCar] = mTime;
cars[history[mCar]].status = 1;
}
else { //Het slot
se.insert(history[mCar]);
cars[history[mCar]].status = 2;
}
}
return se.size();
}
int fee(int time) {
if(time <= baseTime) {
return baseFee;
} else {
return baseFee + ceil((time - baseTime) / unitTime) * unitFee;
}
}
int leave(int mTime, int mCar) {
if(mp.find(mCar) != mp.end()) { //mCar trong Parking
cars[history[mCar]].parkingTime += (mTime - cars[history[mCar]].timeJoin);
cars[history[mCar]].totalParkingTime += cars[history[mCar]].parkingTime;
cars[history[mCar]].status = 0;
mp.erase(mCar);
if(!se.empty()) {
auto idx = se.begin();
int key = cars[*idx].id;
mp[key] = mTime;
cars[key].totalWaitingTime += (mTime - cars[key].timeJoin);
cars[key].totalParkingTime = 0;
cars[key].status = 1;
cars[key].timeJoin = mTime;
}
return fee(cars[history[mCar]].parkingTime);
}
else { //mCar trong waiting
cars[history[mCar]].status = 0;
cars[history[mCar]].timeJoin = 0;
cars[history[mCar]].totalParkingTime = 0;
cars[history[mCar]].totalWaitingTime +=(mTime -cars[history[mCar]].timeJoin);
se.erase(history[mCar]);
return -1;
}
}Editor is loading...
Leave a Comment