import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastInputReader reader = new FastInputReader(System.in);
int n = reader.nextInt(); // Number of days
int d = reader.nextInt(); // Delay allowed
int m = reader.nextInt(); // Number of events
// Use a threshold to decide which approach to use
if (m <= 1000 || n <= 100) {
// Use simpler approach for smaller cases
simpleApproach(reader, n, d, m);
} else {
// Use optimized approach for larger cases
optimizedApproach(reader, n, d, m);
}
}
// Simple approach for smaller cases
private static void simpleApproach(FastInputReader reader, int n, int d, int m) throws IOException {
Pair[] a = new Pair[m + 1];
for (int i = 1; i <= m; i++) {
a[i] = new Pair(reader.nextInt(), i);
}
// Sort using Arrays.sort
Arrays.sort(a, 1, m + 1);
int l = 0, r = m;
while (r - l > 1) {
int mid = (l + r) / 2;
int p = 1;
for (int i = 1; i <= n; i++) {
if (p > m || a[p].fr + d < i) {
break;
}
int cnt = 0;
while (cnt < mid && p <= m && a[p].fr <= i) {
cnt++;
p++;
}
}
if (p > m) {
r = mid;
} else {
l = mid;
}
}
// Output results using BufferedWriter
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(r + "\n");
int p = 1;
for (int i = 1; i <= n; i++) {
int cnt = 0;
while (cnt < r && p <= m && a[p].fr <= i) {
bw.write(a[p].sc + " ");
cnt++;
p++;
}
bw.write("0\n");
}
bw.flush();
}
// Optimized approach for larger cases
private static void optimizedApproach(FastInputReader reader, int n, int d, int m) throws IOException {
int[] fr = new int[m + 1];
int[] sc = new int[m + 1];
for (int i = 1; i <= m; i++) {
fr[i] = reader.nextInt();
sc[i] = i;
}
// Sort by `fr` using in-place quicksort
quickSort(fr, sc, 1, m);
int l = 0, r = m;
while (r - l > 1) {
int mid = (l + r) / 2;
int p = 1;
for (int i = 1; i <= n; i++) {
if (p > m || fr[p] + d < i) {
break;
}
int cnt = 0;
while (cnt < mid && p <= m && fr[p] <= i) {
cnt++;
p++;
}
}
if (p > m) {
r = mid;
} else {
l = mid;
}
}
// Output results using BufferedWriter
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(r + "\n");
int p = 1;
for (int i = 1; i <= n; i++) {
int cnt = 0;
while (cnt < r && p <= m && fr[p] <= i) {
bw.write(sc[p] + " ");
cnt++;
p++;
}
bw.write("0\n");
}
bw.flush();
}
// Optimized quicksort to sort `fr` and rearrange `sc` accordingly
private static void quickSort(int[] fr, int[] sc, int low, int high) {
if (low < high) {
int pivotIndex = partition(fr, sc, low, high);
quickSort(fr, sc, low, pivotIndex - 1);
quickSort(fr, sc, pivotIndex + 1, high);
}
}
private static int partition(int[] fr, int[] sc, int low, int high) {
int pivot = fr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (fr[j] <= pivot) {
i++;
swap(fr, sc, i, j);
}
}
swap(fr, sc, i + 1, high);
return i + 1;
}
private static void swap(int[] fr, int[] sc, int i, int j) {
int tempFr = fr[i];
fr[i] = fr[j];
fr[j] = tempFr;
int tempSc = sc[i];
sc[i] = sc[j];
sc[j] = tempSc;
}
static class Pair implements Comparable<Pair> {
int fr, sc;
Pair(int fr, int sc) {
this.fr = fr;
this.sc = sc;
}
@Override
public int compareTo(Pair other) {
return Integer.compare(this.fr, other.fr);
}
}
static class FastInputReader {
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastInputReader(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[1 << 16]; // 64 KB buffer
bufferPointer = bytesRead = 0;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
bytesRead = din.read(buffer, 0, buffer.length);
bufferPointer = 0;
if (bytesRead == -1) return -1; // End of stream
}
return buffer[bufferPointer++];
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read(); // Skip whitespace
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
}
}
Compilation message
jobs.java:4: error: class Main is public, should be declared in a file named Main.java
public class Main {
^
1 error