Submission #1119552

#TimeUsernameProblemLanguageResultExecution timeMemory
1119552johuJob Scheduling (CEOI12_jobs)Java
65 / 100
560 ms65536 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 { ArrayList<Integer>[] c = new ArrayList[n]; for (int i = 0; i < n; i++) { c[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int t = reader.nextInt() - 1; c[t].add(i); } Pair[] in = new Pair[m]; List<Integer>[] ans = new ArrayList[n]; for (int i = 0; i < n; i++) { ans[i] = new ArrayList<>(); } // Check function translated from C++ Function<Integer, Boolean> check = x -> { int cur = 0, len = 0; for (int i = 0; i < n; i++) { if (cur < len && in[cur].fr <= i) { return false; } int end = i + d + 1; for (int cVal : c[i]) { in[len++] = new Pair(end, cVal); } int left = x; while (left-- > 0 && cur < len) { ans[i].add(in[cur++].sc); } } return cur == len; }; int lo = 1, hi = m; while (lo < hi) { int mid = (lo + hi) / 2; if (check.apply(mid)) { hi = mid; } else { lo = mid + 1; } } check.apply(lo); System.out.println(lo); for (List<Integer> list : ans) { for (int x : list) { System.out.print((x + 1) + " "); } 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; } } }

Compilation message (stderr)

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