제출 #1119553

#제출 시각아이디문제언어결과실행 시간메모리
1119553johuJob Scheduling (CEOI12_jobs)Java
65 / 100
1089 ms56904 KiB
import java.io.*;
import java.util.*;
import java.util.function.Function;

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 {
            // Use the C++ equivalent logic for larger cases
            handleLargeCasesCppEquivalent(reader, n, d, m);
        }
    }

    private static void handleSmallCases(FastInputReader reader, int n, int d, int m) throws IOException {
        List<Pair> a = new ArrayList<>(m + 2);
        for (int i = 1; i <= m; i++) {
            a.add(new Pair(reader.nextInt(), i));
        }
        a.add(new Pair(1000000000, 0)); // Dummy pair for boundary

        a.sort(Comparator.comparingInt(p -> p.fr));

        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 handleLargeCasesCppEquivalent(FastInputReader reader, int n, int d, int m) throws IOException {
    // Use a single array to store indices of jobs at each day
    List<Integer>[] c = new List[n];
    for (int i = 0; i < n; i++) {
        c[i] = new ArrayList<>();
    }

    // Read the input and map jobs to their corresponding days
    for (int i = 0; i < m; i++) {
        int t = reader.nextInt() - 1;
        c[t].add(i);
    }

    // Check function optimized for memory usage
    Function<Integer, Boolean> check = x -> {
        int cur = 0; // Current position in the job queue
        int[] in = new int[m]; // Minimal array for job indices
        int len = 0; // Current size of the job queue

        for (int i = 0; i < n; i++) {
            // Remove expired jobs (those that can't start anymore)
            while (cur < len && in[cur] <= i) {
                cur++;
            }

            // Add new jobs for the current day
            for (int job : c[i]) {
                in[len++] = i + d + 1;
            }

            // Allocate up to `x` jobs for the current day
            int left = x;
            while (left-- > 0 && cur < len) {
                cur++;
            }
        }

        // Valid if all jobs are processed
        return cur == len;
    };

    // Binary search for the minimum number of jobs per day
    int lo = 1, hi = m;
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (check.apply(mid)) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }

    // Re-run the check to create the final answer
    int[] in = new int[m]; // Job queue
    int len = 0, cur = 0;  // Queue size and pointer
    int[][] result = new int[n][]; // Results for each day
    for (int i = 0; i < n; i++) {
        while (cur < len && in[cur] <= i) {
            cur++;
        }
        for (int job : c[i]) {
            in[len++] = i + d + 1;
        }
        int left = lo, count = 0;
        List<Integer> dayResult = new ArrayList<>();
        while (left-- > 0 && cur < len) {
            dayResult.add(in[cur++]);
            count++;
        }
        result[i] = dayResult.stream().mapToInt(x -> x + 1).toArray();
    }

    // Print results
    System.out.println(lo);
    for (int[] dayResult : result) {
        for (int x : dayResult) {
            System.out.print(x + " ");
        }
        System.out.println(0);
    }
}


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

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = 0;

            while (bufferPointer < bytesRead || refillBuffer()) {
                c = buffer[bufferPointer++];
                if (c > ' ') break; // Skip non-digit
            }

            boolean neg = (c == '-');
            if (neg) c = buffer[bufferPointer++];

            while (c >= '0' && c <= '9') {
                ret = ret * 10 + (c - '0');
                if (bufferPointer == bytesRead && !refillBuffer()) break;
                c = buffer[bufferPointer++];
            }
            return neg ? -ret : ret;
        }

        private boolean refillBuffer() throws IOException {
            bytesRead = din.read(buffer, 0, buffer.length);
            bufferPointer = 0;
            return bytesRead > 0;
        }
    }
}

컴파일 시 표준 에러 (stderr) 메시지

Note: jobs.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
#Verdict Execution timeMemoryGrader output
Fetching results...