Untitled

mail@pastecode.io avatar
unknown
plain_text
8 days ago
2.3 kB
4
Indexable
Never
#include <iostream>
#include <set>
#include <unordered_map>

using namespace std;

struct Car{
	int id;
	int tDen;
	int totalParking;
	int totalWaiting;
	int tWait;
	int tPark;
}cars[70001];

struct cmp{
	bool operator()(Car a, Car b) {
		int x = a.totalWaiting - a.totalParking;
		int y = b.totalWaiting - b.totalParking;
		if(x == y) return a.tDen < b.tDen;
		return x > y;
	}
};

unordered_map<int, int>mp;
set<Car, cmp>se;

int bTime, bFee, uTime, uFee, capacity, cnt;

void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) {
	bTime = mBaseTime;
	bFee = mBaseFee;
	uTime = mUnitTime;
	uFee = mUnitFee;
	capacity = mCapacity;
	cnt = 0;

	mp.clear();
	se.clear();
}

int arrive(int mTime, int mCar) {
	if(mp.find(mCar) != mp.end()) { //Chua tung xuat hien
		if(capacity) { //Neu con slot
			capacity--;
			mp[mCar] = cnt;
			cars[cnt].id = mCar;
			cars[cnt].tDen = mTime;
			cars[cnt].totalParking = 0;
			cars[cnt].totalWaiting = 0;
			cars[cnt].tPark = 0;
			cars[cnt].tWait = 0;
			cnt++;
		} 
		else { // Het slot
			mp[mCar] = cnt;
			cars[cnt].id = mCar;
			cars[cnt].tDen = mTime;
			cars[cnt].totalParking = 0;
			cars[cnt].totalWaiting = 0;
			cars[cnt].tPark = 0;
			cars[cnt].tWait = mTime;
			se.insert(cars[cnt]);
			cnt++;
		}
	} 
	else { //Da tung xuat hien
		int idx = mp[mCar];
		if(capacity) { //Neu con slot
			capacity--;
			cars[idx].tDen = mTime;
			cars[idx].tPark = mTime;
		} 
		else { //Neu het slot
			cars[idx].tDen = mTime;
			cars[idx].tWait = mTime;
			se.insert(cars[idx]);
		}
	}
	return se.size();
}

int leave(int mTime, int mCar) {
	int ans = 0;
	int idx = mp[mCar];
	if(se.find(cars[idx]) == se.end()) { //Trong parking
		capacity++;
		cars[idx].totalParking += mTime - cars[idx].tPark;

		//Tinh cost
		int timePark = mTime - cars[idx].tPark;
		if(timePark <= bTime) ans = bFee;
		else {
			int timeN = timePark / bTime;
			int timeMod = timePark % bTime;
			if(timeMod > 0) timeN++;
			ans = bFee + timeN * uFee;
		}
		//Neu con xe waiting
		if(!se.empty()) {
			auto topCar = se.begin();
			int idCar = topCar->id;
			int idxCar = mp[idCar];
			cars[idxCar].tPark = mTime;
		} else {
			capacity++;
		}
	}
	return 0;
}
Leave a Comment