CSES - Shared codeLink to this code:
https://cses.fi/paste/93c93ad5e9b46a4f143f6e/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] result = new int[n];
int[][] arr = new int[n][3];
for(int i = 0; i<n; i++) {
arr[i][0] = scn.nextInt();
arr[i][1] = scn.nextInt();
arr[i][2] = i;
}
Arrays.sort(arr, (x,y) -> Integer.compare(x[0], y[0]));
Queue<int[]> pq = new PriorityQueue<>(n,(x,y) -> Integer.compare(x[0], y[0]));
int[] room;
for(int[] cus: arr) {
if(!pq.isEmpty() && cus[0] > pq.peek()[0]) {
room = pq.remove();
room[0] = cus[1];
}else {
room = new int[2];
room[0] = cus[1];
room[1] = pq.size()+1;
}
pq.add(room);
result[cus[2]] = room[1];
}
System.out.println(pq.size());
for(int num: result) {
System.out.print(num + " ");
}
}
}