제출 #675371

#제출 시각아이디문제언어결과실행 시간메모리
675371wisePigOnTheMoonJob Scheduling (CEOI12_jobs)Java
70 / 100
403 ms48772 KiB
import java.util.*;
import java.io.*;

public class jobs {
    private static final int DAY = 1, ID = 0;
    private static class Node {
        public Node next;
        public int id;

        public Node(int id) {
            this.id = id;
            next = null;
        }
    }
    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];
        Node[] ids = new Node[days];
        for (int i = 0; i < totalJobs; i++) {
            int requestDay = in.nextInt() - 1;
            jobs[requestDay]++;
            Node node = new Node(i + 1);
            if (ids[requestDay] == null) {
                ids[requestDay] = node;
            } else {
                node.next = ids[requestDay];
                ids[requestDay] = node;
            }
        }
        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, Node[] ids) {
        StringBuilder sb = new StringBuilder();
        int done = 0, start = 0, nid = 0;
        for (int current = 0; current < days; current++) {
           while (done < numMachines && start <= current) {
               if (ids[start] == null) {
                   start++;
                   continue;
               }
               Node node = ids[start];
               ids[start] = node.next;
               sb.append(node.id);
               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;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...