240517 랭킹전 대기열

Jongleee·2024년 5월 17일
0

TIL

목록 보기
575/737
static StringBuilder sb;

static class Room {
	int cnt;
	int firstLevel;
	Queue<User> list;

	public Room(int cnt, int firstLevel) {
		this.cnt = cnt;
		this.firstLevel = firstLevel;
		this.list = new PriorityQueue<>();
	}
}

static class User implements Comparable<User> {
	String name;
	int level;

	public User(String name, int level) {
		this.name = name;
		this.level = level;
	}

	@Override
	public int compareTo(User o) {
		return this.name.compareTo(o.name);
	}
}

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	sb = new StringBuilder();

	StringTokenizer st = new StringTokenizer(br.readLine());
	int p = Integer.parseInt(st.nextToken());
	int m = Integer.parseInt(st.nextToken());
	List<Room> roomList = new ArrayList<>();

	for (int i = 0; i < p; i++) {
		st = new StringTokenizer(br.readLine());
		int level = Integer.parseInt(st.nextToken());
		String name = st.nextToken();
		boolean isEnter = false;

		for (Room room : roomList) {
			if (room.cnt < m && room.firstLevel + 10 >= level && room.firstLevel - 10 <= level) {
				isEnter = true;
				room.list.add(new User(name, level));
				room.cnt++;
				break;
			}
		}

		if (!isEnter) {
			Room room = new Room(1, level);
			room.list.add(new User(name, level));
			roomList.add(room);
		}
	}

	for (Room room : roomList) {
		if (room.cnt == m) {
			sb.append("Started!\n");
		} else {
			sb.append("Waiting!\n");
		}

		while (!room.list.isEmpty()) {
			User user = room.list.poll();
			sb.append(user.level).append(" ").append(user.name).append("\n");
		}
	}

	System.out.print(sb.toString());
}

출처:https://www.acmicpc.net/problem/20006

0개의 댓글