import java.io.*;
public class jobs {
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
if (m <= 300000) {
// Use the ArrayList-based implementation for smaller cases
handleSmallCases(reader, n, d, m);
} else {
// For larger cases, use optimized quicksort-based implementation with DataInputStream
handleLargeCases(n, d, m, reader);
}
}
private static void handleSmallCases(FastInputReader reader, int n, int d, int m) throws IOException {
int[] fr = new int[m];
int[] sc = new int[m];
for (int i = 0; i < m; i++) {
fr[i] = reader.nextInt();
sc[i] = i + 1;
}
quickSort(fr, sc, 0, m - 1);
int l = 0, r = m + 1;
while (r - l > 1) {
int mid = (l + r) / 2;
int p = 0;
boolean valid = true;
for (int i = 1; i <= n; i++) {
int cnt = 0;
while (cnt < mid && p < m && fr[p] <= i) {
cnt++;
p++;
}
if (p == m && i < n) {
valid = false;
break;
}
}
if (valid && p >= m) {
r = mid;
} else {
l = mid;
}
}
System.out.println(r);
StringBuilder sb = new StringBuilder();
int p = 0;
for (int i = 1; i <= n; i++) {
int cnt = 0;
while (cnt < r && p < m && fr[p] <= i) {
sb.append(sc[p]).append(" ");
cnt++;
p++;
}
sb.append("0\n");
}
System.out.print(sb);
}
private static void handleLargeCases(int n, int d, int m, FastInputReader reader) throws IOException {
int[] fr = new int[m];
int[] sc = new int[m];
for (int i = 0; i < m; i++) {
fr[i] = reader.nextInt();
sc[i] = i + 1;
}
quickSort(fr, sc, 0, m - 1);
int l = 0, r = m + 1;
while (r - l > 1) {
int mid = (l + r) / 2;
int p = 0;
boolean valid = true;
for (int i = 1; i <= n; i++) {
int cnt = 0;
while (cnt < mid && p < m && fr[p] <= i) {
cnt++;
p++;
}
if (p == m && i < n) {
valid = false;
break;
}
}
if (valid && p >= m) {
r = mid;
} else {
l = mid;
}
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(r + "\n");
int p = 0;
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();
}
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 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;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1043 ms |
13764 KB |
Time limit exceeded |
2 |
Execution timed out |
1055 ms |
13568 KB |
Time limit exceeded |
3 |
Execution timed out |
1054 ms |
13772 KB |
Time limit exceeded |
4 |
Execution timed out |
1033 ms |
13452 KB |
Time limit exceeded |
5 |
Execution timed out |
1051 ms |
13888 KB |
Time limit exceeded |
6 |
Execution timed out |
1068 ms |
13700 KB |
Time limit exceeded |
7 |
Execution timed out |
1047 ms |
13292 KB |
Time limit exceeded |
8 |
Execution timed out |
1066 ms |
13532 KB |
Time limit exceeded |
9 |
Incorrect |
854 ms |
17284 KB |
Output isn't correct |
10 |
Incorrect |
633 ms |
16784 KB |
Output isn't correct |
11 |
Incorrect |
187 ms |
15132 KB |
Output isn't correct |
12 |
Incorrect |
284 ms |
18760 KB |
Output isn't correct |
13 |
Incorrect |
311 ms |
20940 KB |
Output isn't correct |
14 |
Incorrect |
331 ms |
19716 KB |
Output isn't correct |
15 |
Incorrect |
692 ms |
21948 KB |
Output isn't correct |
16 |
Incorrect |
377 ms |
22868 KB |
Output isn't correct |
17 |
Incorrect |
430 ms |
26320 KB |
Output isn't correct |
18 |
Execution timed out |
1053 ms |
18192 KB |
Time limit exceeded |
19 |
Execution timed out |
1060 ms |
19116 KB |
Time limit exceeded |
20 |
Incorrect |
441 ms |
26380 KB |
Output isn't correct |