Untitled
unknown
plain_text
14 days ago
2.8 kB
3
Indexable
Never
#include <unordered_map> #include <set> #include <cmath> #include <vector> #include <algorithm> using namespace std; struct Car { int id; int status; int timeJoin; int parkingTime; int totalParkingTime; int totalWaitingTime; int val() const { return totalWaitingTime - totalParkingTime; } }; vector<Car> cars; struct Cmp { bool operator()(int a, int b) const { const Car& car1 = cars[a]; const Car& car2 = cars[b]; if (car1.val() != car2.val()) return car1.val() > car2.val(); return car1.timeJoin < car2.timeJoin; } }; unordered_map<int, int> mp; set<int, Cmp> se; unordered_map<int, int> history; int baseTime, baseFee, unitTime, unitFee, capacity; void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) { se.clear(); mp.clear(); history.clear(); cars.clear(); baseTime = mBaseTime; baseFee = mBaseFee; unitFee = mUnitFee; unitTime = mUnitTime; capacity = mCapacity; } int arrive(int mTime, int mCar) { auto it = history.find(mCar); if (it == history.end()) { // New car Car newCar{mCar, 0, mTime, 0, 0, 0}; cars.push_back(newCar); history[mCar] = cars.size() - 1; } int carIndex = history[mCar]; Car& car = cars[carIndex]; if (mp.size() < capacity) { // Parking lot has space mp[mCar] = mTime; car.status = 1; // 1: in parking lot car.timeJoin = mTime; } else { // Parking lot is full car.status = 2; // 2: in waiting queue car.timeJoin = mTime; se.insert(carIndex); } return se.size(); } int fee(int time) { if (time <= baseTime) return baseFee; return baseFee + ceil((double)(time - baseTime) / unitTime) * unitFee; } int leave(int mTime, int mCar) { auto it = history.find(mCar); if (it == history.end()) return -1; // Car not found int carIndex = it->second; Car& car = cars[carIndex]; if (mp.find(mCar) != mp.end()) { // Car is in parking lot car.parkingTime += (mTime - car.timeJoin); car.totalParkingTime += car.parkingTime; car.status = 0; mp.erase(mCar); // Update waiting queue if (!se.empty()) { int nextCarIndex = *se.begin(); Car& nextCar = cars[nextCarIndex]; mp[nextCar.id] = mTime; nextCar.totalWaitingTime += (mTime - nextCar.timeJoin); nextCar.status = 1; nextCar.timeJoin = mTime; se.erase(se.begin()); } return fee(car.parkingTime); } else { // Car is in waiting queue car.totalWaitingTime += (mTime - car.timeJoin); car.status = 0; car.timeJoin = 0; se.erase(carIndex); return -1; } }
Leave a Comment