Submission #675369

#TimeUsernameProblemLanguageResultExecution timeMemory
675369wisePigOnTheMoonJob Scheduling (CEOI12_jobs)Java
65 / 100
559 ms65536 KiB
import java.util.*;
import java.io.*;

public class jobs {
    private static final int DAY = 1, ID = 0;
    public static void main(String[] args) throws Exception {
        Reader in = new Reader();
        int days = in.nextInt();
        int d = in.nextInt();
        int totalJobs = in.nextInt();
        int[] jobs = new int[days];
        ArrayList[] ids = new ArrayList[days];
        for (int i = 0; i < days; i++) {
            ids[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < totalJobs; i++) {
            int requestDay = in.nextInt() - 1;
            jobs[requestDay]++;
            ids[requestDay].add(i+1);
        }
        for (int i = 0; i < days; i++) {
            ids[i].trimToSize();
        }
        int l = 1;
        int r = totalJobs;
        while (l < r) {
            int mid = (l + r)/2;
            if (checkValid(mid, totalJobs, days, d, jobs)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        System.out.println(l);
        printSchedule(l, days, ids);
    }

    public static boolean checkValid(int numMachines, int totalJobs, int days, int d,  int[] jobs) {
        int start = 0, current = 0, toDo = jobs[start], machines = numMachines;
        int done = 0;
        while (current < days && (current - start) <= d) {
            if (toDo > machines) {
                done += machines;
                toDo -= machines;
                current++;
                machines = numMachines;
            } else {
                machines -= toDo;
                done += toDo;
                if (machines == 0) {
                    current++;
                    machines = numMachines;
                }
                start++;
                toDo = start < days ? jobs[start] : 0;
            }
            if (start > current) {
                current = start;
                machines = numMachines;
            }
        }
        if (current - start > d || done < totalJobs) {
            return false;
        }
        return true;
    }

    public static void printSchedule(int numMachines, int days, ArrayList[] ids) {
        StringBuilder sb = new StringBuilder();
        int done = 0, start = 0, lid = 0;
        for (int current = 0; current < days; current++) {
           while (done < numMachines && start <= current) {
               if (lid >= ids[start].size()) {
                   start++;
                   lid = 0;
                   continue;
               }
               sb.append(ids[start].get(lid++));
               sb.append(" ");
               done++;
           }
           sb.append("0\n");
           done = 0;
        }
        System.out.println(sb.toString());
    }

    static class Reader //FastReader//
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            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 (stderr)

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