Submission #1119503

# Submission time Handle Problem Language Result Execution time Memory
1119503 2024-11-27T05:38:17 Z johu Job Scheduling (CEOI12_jobs) Java 11
77 / 100
1000 ms 30632 KB
import java.io.*;
import java.util.*;

public class jobs {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        if (m <= 300000) {
            // Use the ArrayList-based implementation for smaller cases
            handleSmallCases(br, n, d, m);
        } else {
            // Use the large case code exactly as provided
            handleLargeCases(n, d, m);
        }
    }

    private static void handleSmallCases(BufferedReader br, int n, int d, int m) throws IOException {
        List<Pair> a = new ArrayList<>(m + 2);
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= m; i++) {
            a.add(new Pair(Integer.parseInt(st.nextToken()), i));
        }
        a.add(new Pair(1000000000, 0)); // Dummy pair for boundary

        // Sort using built-in sort with natural order
        a.sort(Comparator.naturalOrder());

        int l = 0, r = m;

        while (r - l > 1) {
            int mid = (l + r) / 2;
            int p = 0;

            for (int i = 1; i <= n; i++) {
                if (a.get(p).fr + d < i) {
                    break;
                }
                int cnt = 0;
                while (cnt < mid && p < m && a.get(p).fr <= i) {
                    cnt++;
                    p++;
                }
            }

            if (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 && a.get(p).fr <= i) {
                sb.append(a.get(p).sc).append(" ");
                cnt++;
                p++;
            }
            sb.append("0\n");
        }

        System.out.print(sb);
    }

    private static void handleLargeCases(int n, int d, int m) throws IOException {
        // Invoke the large case code directly
        LargeCaseHandler.main(n, d, m);
    }

    // Wrapper class for the large case code
    static class LargeCaseHandler {
        public static void main(int n, int d, int m) throws IOException {
            FastInputReader reader = new FastInputReader(System.in);

            // Use separate arrays for `fr` and `sc`
            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;

            // Binary search for the minimum value of `r`
            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
            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();
        }

        // Custom 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 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;
            }
        }
    }

    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);
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 358 ms 19228 KB Output is correct
2 Correct 362 ms 19136 KB Output is correct
3 Correct 332 ms 19080 KB Output is correct
4 Correct 324 ms 18836 KB Output is correct
5 Correct 321 ms 19052 KB Output is correct
6 Correct 338 ms 19240 KB Output is correct
7 Correct 330 ms 19004 KB Output is correct
8 Correct 348 ms 18884 KB Output is correct
9 Correct 591 ms 19292 KB Output is correct
10 Correct 524 ms 19476 KB Output is correct
11 Correct 513 ms 20812 KB Output is correct
12 Correct 531 ms 25300 KB Output is correct
13 Correct 706 ms 30632 KB Output is correct
14 Partially correct 394 ms 20140 KB Partially correct
15 Partially correct 711 ms 21908 KB Partially correct
16 Partially correct 441 ms 23436 KB Partially correct
17 Partially correct 460 ms 26196 KB Partially correct
18 Partially correct 964 ms 28300 KB Partially correct
19 Execution timed out 1050 ms 29340 KB Time limit exceeded
20 Partially correct 449 ms 25972 KB Partially correct