# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1104966 | APerson | Job Scheduling (CEOI12_jobs) | Java | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d = sc.nextInt();
int m = sc.nextInt();
ArrayList<ArrayList<Integer>> days = new ArrayList<>();
for(int i = 0; i < n; i++) days.add(new ArrayList<>());
for(int i = 1; i <= m; i++) {
days.get(sc.nextInt()).add(i);
}
// turn input into a list where they are all just the days of the jobs
ArrayList<Integer> jobs = new ArrayList<>();
ArrayList<Integer> forPrinting = new ArrayList<>();
for(int i = 0; i < n; i++) {
forPrinting.addAll(days.get(i));
for(int j = 0; j < days.get(i).size(); j++) {
jobs.add(i);
}
}
Collections.reverse(forPrinting);
// binary search on answer
int min = 1;
int max = m;
while(min != max) {
int mid = (min + max)/2;
if(works(mid, d, jobs)) {
max = mid;
} else {
min = mid + 1;
}
}
// output answer
System.out.println(min);
for(int i = 0; i < n; i++) {
int index = min;
while(index > 0 && !forPrinting.isEmpty()) {
System.out.print(forPrinting.remove(forPrinting.size() - 1) + " ");
index--;
}
System.out.println(0);
}
sc.close();
}
public static boolean works(int mid, int d, ArrayList<Integer> days) {
int[] lastUsed = new int[mid];
int index = 0;
int curDay = 0;
for (int day : days) {
index %= mid;
if(index == 0) curDay++;
lastUsed[index] = day;
if(curDay - lastUsed[index] > d) return false;
index++;
}
return true;
}
}