Untitled
unknown
plain_text
a year ago
3.7 kB
9
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) const {
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;
}
int arrive(int mTime, int mCar) {
if (history.find(mCar) == history.end()) { // Chưa từng vào bãi đỗ
cnt++;
history[mCar] = cnt;
if (mp.size() < capacity) { // Còn slot
mp[mCar] = mTime;
cars[history[mCar]].id = mCar;
cars[history[mCar]].status = 1; // 1: trong bãi đỗ, 2: trong hàng đợi
cars[history[mCar]].timeJoin = mTime;
cars[history[mCar]].parkingTime = 0;
cars[history[mCar]].totalParkingTime = 0;
cars[history[mCar]].totalWaitingTime = 0;
} else { // Hết slot
cars[history[mCar]].id = mCar;
cars[history[mCar]].status = 2; // Xe vào hàng chờ
cars[history[mCar]].timeJoin = mTime;
cars[history[mCar]].parkingTime = 0;
cars[history[mCar]].totalParkingTime = 0;
cars[history[mCar]].totalWaitingTime = 0;
se.insert(cnt);
}
} else { // Đã từng vào bãi đỗ
if (mp.size() < capacity) { // Còn slot
mp[mCar] = mTime;
cars[history[mCar]].status = 1; // Trở lại bãi đỗ
} else { // Hết slot
se.insert(history[mCar]);
cars[history[mCar]].status = 2; // Vào hàng chờ
}
}
return se.size();
}
int fee(int time) {
if (time <= baseTime) {
return baseFee;
} else {
return baseFee + ceil((double)(time - baseTime) / unitTime) * unitFee;
}
}
int leave(int mTime, int mCar) {
if (mp.find(mCar) != mp.end()) { // Xe đang trong bãi đỗ
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);
// Cập nhật hàng đợi
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;
se.erase(idx); // Xóa xe ra khỏi hàng đợi
}
return fee(cars[history[mCar]].parkingTime);
} else if (se.find(history[mCar]) != se.end()) { // Xe trong hàng đợi
cars[history[mCar]].totalWaitingTime += (mTime - cars[history[mCar]].timeJoin);
cars[history[mCar]].status = 0;
cars[history[mCar]].timeJoin = 0;
cars[history[mCar]].totalParkingTime = 0;
se.erase(history[mCar]);
return -1;
} else {
return -1; // Xe không có trong parking hoặc hàng đợi
}
}
Editor is loading...
Leave a Comment