Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
2.8 kB
3
Indexable
Never
#include<iostream>
#include<set>
#include<unordered_map>
using namespace std ;
struct CAR
{
    int id ;
    int timeJoin ;
    int total;
    int val()
    {
        return total - timeJoin ;
    }
    int status ; // status = 1 la dang do , 2 la dang cho , 0 la khoi tao
};
CAR carPool[70001];
struct cmp
{
    bool operator ()(int a , int b)
    {
        int v1 = carPool[a].val();
        int v2 = carPool[b].val();
        if (v1!= v2)
            return v1 >v2 ;
        return carPool[a].timeJoin < carPool[b].timeJoin ;
    }
};
unordered_map<int ,int> carMap ;
set<int ,cmp > waitingLine ;
 
int baseTime, baseFee,unitTime,unitFee ;
int capacity ;
int currCap ;
int carNo ;
 
void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee, int mCapacity) {
    baseTime = mBaseTime ;
    baseFee = mBaseFee ;
    unitTime = mUnitTime ;
    unitFee = mUnitFee ;
    capacity = mCapacity ;
     
    currCap = 0 ;
    carNo = 0 ;
    carMap.clear();
    waitingLine.clear();
    return;
}
 
int arrive(int mTime, int mCar) {
    // car moi 
    if (!carMap.count(mCar))
    {
        carNo ++ ;
        carMap[mCar] = carNo ;
        carPool[carNo].id = mCar ;
        carPool[carNo].total = 0 ;
        carPool[carNo].status = 0 ;
        carPool[carNo].timeJoin = 0 ;
 
    }
    int tmpID = carMap[mCar];
    carPool[tmpID].timeJoin = mTime ;
    if (currCap >= capacity)
    {
        carPool[tmpID].status = 2 ;
        waitingLine.insert(tmpID);
    }
    else
    {
        currCap++;
        carPool[tmpID].status = 1 ;
    }
    return waitingLine.size();
}
int calFee(int time)
{
    if (time <= baseTime)
        return baseFee ;
    int res ; 
    if ((time - baseTime) % unitTime == 0)
    {
        res = (time - baseTime)/unitTime * unitFee + baseFee ; 
    }
    else
        res = ((time - baseTime)/unitTime +1) * unitFee + baseFee ; 
    return res ;
     
}
int leave(int mTime, int mCar) {
    int tmpID = carMap[mCar]  ;
    if (carPool[tmpID].status == 1)
    {
        int res = calFee(mTime - carPool[tmpID].timeJoin);
        carPool[tmpID].total -=( mTime - carPool[tmpID].timeJoin) ;
        currCap -- ;
 
        if (currCap < capacity && !waitingLine.empty())
        {
            auto pos = waitingLine.begin();
            int tmpID2 = *pos;
            carPool[tmpID2].status = 1 ;
            carPool[tmpID2].total += (mTime - carPool[tmpID2].timeJoin );
            carPool[tmpID2].timeJoin = mTime ;
            waitingLine.erase(pos);
            currCap++;
        }
        return res ;
    }
    auto pos2 = waitingLine.find(tmpID);
    waitingLine.erase(pos2);
    carPool[tmpID].total += (mTime - carPool[tmpID].timeJoin);
    return -1;
}
Leave a Comment