Untitled
unknown
plain_text
a year ago
2.4 kB
14
Indexable
#include <unordered_map>
#include <set>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
int baseTime, baseFee, unitTime, unitFee, capacity;
struct cmp {
bool operator() (pair<int, int> a, pair<int, int> b) const {
return a.second < b.second;
}
};
unordered_map<int, int> mp; // Bãi đỗ xe (Car -> Arrival time)
set<pair<int, int>, cmp> se; // Hàng đợi xe (Car -> Arrival time)
unordered_map<int, int> q; // Hàng đợi xe (Car -> Arrival time)
void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) {
mp.clear();
se.clear();
q.clear();
baseFee = mBaseFee;
baseTime = mBaseTime;
unitTime = mUnitTime;
unitFee = mUnitFee;
capacity = mCapacity;
}
//60 5000 20 300 5
int arrive(int mTime, int mCar) {
if (mp.size() < capacity) { // Có chỗ trống trong bãi đỗ
//cout << mp.size() << endl;
mp[mCar] = mTime;
//ut << "mp.size: " << mp.size() << endl;
}
else { // Không có chỗ trống, đưa vào hàng đợi
se.insert(make_pair(mCar, mTime));
cout << "Begin: " << se.begin()->first << endl;
q[mCar] = mTime;
}
//out << "Se.size: "<<se.size() << endl;
return se.size(); // Trả về số lượng xe trong hàng đợi
}
int leave(int mTime, int mCar) {
double cost = 0;
if (mp.find(mCar) != mp.end()) { // Xe đang ở trong bãi đỗ
int timeCar = mp[mCar];
mp.erase(mCar);
if (!se.empty()) {
auto it = se.begin();
int key = it->first;
int value = it->second;
mp[key] = value;
se.erase(it);
q.erase(key);
}
// Tính chi phí
if (mTime - timeCar <= baseTime) {
cost = baseFee;
}
else {
cost = baseFee + ceil(double(mTime - timeCar - baseTime) / unitTime) * unitFee;
}
}
else if (q.find(mCar) != q.end()) { // Xe đang trong hàng đợi
int timeCar = q[mCar];
se.erase(make_pair(mCar, timeCar));
q.erase(mCar);
cost = -1; // Xe trong hàng đợi không bị tính phí, chỉ bị xóa
}
return int(cost); // Trả về chi phí dưới dạng số
}
Editor is loading...
Leave a Comment