Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
1
Indexable
Never
#include<set>
#include<unordered_map>
#include<queue>
using namespace std;

#define NONE 0
#define PARKING 1
#define WATTING 2

struct car{
	int state;
	int id;
	int start_ParkingTime;
	int total_ParkingTime;
	int start_WatingTime;
	int total_WatingTime;
	car(){
		start_ParkingTime=0;
		total_ParkingTime=0;
		start_ParkingTime=0;
		total_WatingTime=0;
	}
};
struct cmp{

	bool operator()(car a,car b)
	{
		int p1 = a.total_WatingTime-a.start_WatingTime-a.total_ParkingTime;
		int p2 = b.total_WatingTime-b.start_WatingTime-b.total_ParkingTime;
		if(p1!=p2)
			return p1>p2;
		return a.start_WatingTime<b.start_WatingTime;
	}
};
unordered_map<int,car> list_car;
set<car,cmp> wating;
int BaseTime;
int BaseFree;
int UnitTime;
int UnitFree;
int Capacity;
void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) {
	BaseTime = mBaseTime;
	BaseFree = mBaseFee;
	UnitTime = mUnitTime;
	UnitFree = mUnitFee;
	Capacity = mCapacity;
	list_car.clear();
	wating.clear();
	return;
}
int arrive(int mTime, int mCar) {
	car newcar ;
	if(list_car.find(mCar)!=list_car.end())
	newcar = list_car[mCar];
	newcar.id=mCar;
	if(Capacity>0){
		Capacity-=1;
		newcar.start_ParkingTime=mTime;
		newcar.state = PARKING;
	}
	else{
		newcar.state=WATTING;
		newcar.start_WatingTime = mTime;
		wating.insert(newcar);
	}
	list_car[mCar]=newcar;
	return wating.size();
}

int caculator(int time)
{
	if(time<=BaseTime)
		return BaseFree;
	else
	{
		return BaseFree+(time-BaseTime+UnitTime-1)/UnitTime*UnitFree;
	}
}
int leave(int mTime, int mCar) {
	auto newcar1 = list_car.find(mCar);
	if(newcar1->second.state==WATTING)
	{
		car newcar = newcar1->second;
		wating.erase(newcar);
		newcar1->second.state=NONE;
		newcar1->second.total_WatingTime += mTime-newcar1->second.start_WatingTime;
		return -1;
	}
	newcar1->second.state=NONE;
	newcar1->second.total_ParkingTime+=mTime-newcar1->second.start_ParkingTime;
	Capacity+=1;
	if(!wating.empty())
	{
		car temp = *wating.begin();
		wating.erase(*wating.begin());
		list_car.erase(temp.id);
		Capacity-=1;
		temp.state=PARKING;
		temp.start_ParkingTime = mTime;
		temp.total_WatingTime +=mTime-temp.start_WatingTime;
		list_car.insert(make_pair(temp.id,temp));
		
	}
	
	return caculator(mTime-newcar1->second.start_ParkingTime);
}