Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
2
Indexable
Never
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.TreeSet;

class UserSolution {
	class Car implements Comparable<Car> {
		int ID;
		int time_parking;
		int time_arr;
		int time_wait;

		public Car() {
			this.time_parking = 0;
			this.time_arr = time_arr;
			this.time_wait = 0;
		}

		@Override
		public int compareTo(Car o) {
			int a = time_wait - time_arr - time_parking;
			int b = o.time_wait - o.time_arr - o.time_parking;
			if (a == b) {
				return time_arr - o.time_arr;
			}
			return b - a;
		}

	}

	static double BaseTime, BaseFee, UnitTime, UnitFee, Capacity;
	HashMap<Integer, Car> park = new HashMap<Integer, UserSolution.Car>();
	HashMap<Integer, Car> carMap = new HashMap<Integer, UserSolution.Car>();
//	PriorityQueue<Car> carWait = new PriorityQueue<UserSolution.Car>();
	TreeSet<Car> carWait=new TreeSet<UserSolution.Car>();

	public void init(int mBaseTime, int mBaseFee, int mUnitTime, int mUnitFee,
			int mCapacity) {
		park.clear();
		carMap.clear();
		carWait.clear();
		BaseFee = mBaseFee;
		BaseTime = mBaseTime;
		UnitFee = mUnitFee;
		UnitTime = mUnitTime;
		Capacity = mCapacity;
	}

	public int arrive(int mTime, int mCar) {
		Car car = new Car();
		if (carMap.containsKey(mCar)) {
			car = carMap.get(mCar);
			car.time_arr = mTime;
		} else {
			car.ID = mCar;
			car.time_arr = mTime;
		}
		if (park.size() < Capacity) {
			park.put(car.ID, car);
			carMap.put(car.ID, car);
		} else {
			carWait.add(car);
			carMap.put(car.ID, car);
		}

		return carWait.size();
	}

	public int leave(int mTime, int mCar) {
		Car car = carMap.get(mCar);
		if (park.containsKey(mCar)) {
			park.remove(mCar);
			car.time_parking += mTime - car.time_arr;
			if (!carWait.isEmpty()) {
				Car car2 = carWait.pollFirst();
//				carWait.remove(car2);
				car2.time_wait += mTime - car2.time_arr;
				car2.time_arr = mTime;
				park.put(car2.ID, car2);
			}
			if (mTime - car.time_arr <= BaseTime) {
				return (int) BaseFee;
			}
			int ans = (int) Math.round(BaseFee
					+ Math.ceil(((mTime - car.time_arr - BaseTime) / UnitTime))
					* UnitFee);
			return ans;
		} else {
			carWait.remove(car);
			car.time_wait += mTime - car.time_arr;
			return -1;

		}
	}
}