Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.6 kB
1
Indexable
Never
package H2122;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Queue;

class UserSolution {
	final static int MAXN = 100;
	final static int MAXL = 8;

	class User {
		int TicketTime, BikeTime, TimePick = -1;
	}

	long tolong(char s[]) {
		long h = 0;
		for (int i = 0; s[i] != 0; i++) {
			h = h << 5 | (s[i] - 'a' + 1);
		}
		return h;
	}

	class Station {
		int DeliTime;
		Queue<Integer> BikeList = new PriorityQueue<Integer>();
		Queue<Integer> queue = new ArrayDeque<>();

		void init(int delitime) {
			this.DeliTime = delitime;
			BikeList.clear();
			queue.clear();
		}

		void add(int utime, int ctime) {
			if (utime > DurableTime) {
				queue.add(ctime + DeliTime);
			} else {
				BikeList.add(utime);
			}
		}

		void sync(int time) {
			if (queue.size() > 0 && queue.peek() <= time) {
				BikeList.add(0);
				queue.poll();
			}
		}
	}

	Station sta[] = new Station[101];
	HashMap<Long, User> useMap = new HashMap<>();
	int DurableTime;

	public void init(int N, int durableTime, int deliveryTimes[]) {
		DurableTime = durableTime;
		for (int i = 0; i < N; i++) {
			if (sta[i] == null) {
				sta[i] = new Station();
			}
			sta[i].init(deliveryTimes[i]);
		}
		useMap.clear();
	}

	public void addBicycle(int cTimestamp, int pID, int bicycleNum) {
		for (int i = 0; i < bicycleNum; i++) {
			sta[pID].BikeList.add(0);
		}
	}

	private User getUser(char[] name) {
		return useMap.computeIfAbsent(tolong(name), k -> new User());
	}

	public void buyTicket(int cTimestamp, char uName[], int validTime) {
		User u = getUser(uName);
		if (u.TicketTime <= cTimestamp) {
			u.TicketTime = cTimestamp + validTime;
		} else {
			u.TicketTime += validTime;
		}
	}

	String toString(char[] tag) {
		String str = "";
		for (int i = 0; tag[i] != '\0'; i++)
			str += tag[i];
		return str;
	}

	public int rentBicycle(int cTimestamp, char uName[], int pID) {
		User u = getUser(uName);
		Station station = sta[pID];
		station.sync(cTimestamp);
		if (u.TimePick != -1 || u.TicketTime <= cTimestamp || station.BikeList.isEmpty()) {
			return -1;
		}
		u.TimePick = cTimestamp;
		return u.BikeTime = station.BikeList.poll();
	}

	public int returnBicycle(int cTimestamp, char uName[], int pID) {
		User u = getUser(uName);
		if (u.TimePick == -1) {
			return -1;
		}
		sta[pID].add(cTimestamp - u.TimePick + u.BikeTime, cTimestamp);
		u.TimePick = -1;
		return u.TicketTime > cTimestamp ? 0 : cTimestamp - u.TicketTime + 1;
	}
}
Leave a Comment