summaryrefslogtreecommitdiff
path: root/src/main/JobQueue.java
blob: 21284f9049b3e2cec4272d8714711dc6f9a1adde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class JobQueue {
    int numWorkers;
    int[] jobs;

    int[] assignedWorker;
    long[] startTime;

    private FastScanner in;
    private PrintWriter out;

    class TimeWorkerPair implements Comparable<TimeWorkerPair> {
        long timerTally;
        int workerID;

        TimeWorkerPair(long timerTally, int workerID) {
            this.timerTally = timerTally;
            this.workerID = workerID;
        }

        @Override
        public int compareTo(TimeWorkerPair that) {
            if (this.timerTally > that.timerTally)
                return 1;
            if (this.timerTally == that.timerTally)
                if (this.workerID > that.workerID)
                    return 1;
                else
                    return 0;
            return -1;
        }
    }

    public static void main(String[] args) throws IOException {
        new JobQueue().solve();
    }

    private void readData() throws IOException {
        numWorkers = in.nextInt();
        int m = in.nextInt();
        jobs = new int[m];
        for (int i = 0; i < m; ++i) {
            jobs[i] = in.nextInt();
        }
    }

    private void writeResponse(int[] assignedWorker, long[] startTime) {
        for (int i = 0; i < jobs.length; ++i) {
            out.println(assignedWorker[i] + " " + startTime[i]);
        }
    }

    void assignJobs() {
        // This is the naive and slow algorithm
        assignedWorker = new int[jobs.length];
        startTime = new long[jobs.length];
        long[] nextFreeTime = new long[numWorkers];
        for (int i = 0; i < jobs.length; i++) {
            int duration = jobs[i];
            int bestWorker = 0;
            for (int j = 0; j < numWorkers; ++j) {
                if (nextFreeTime[j] < nextFreeTime[bestWorker])
                    bestWorker = j;
            }
            assignedWorker[i] = bestWorker;
            startTime[i] = nextFreeTime[bestWorker];
            nextFreeTime[bestWorker] += duration;
        }
    }

    void assignJobsImproved() {
        assignedWorker = new int[jobs.length];
        startTime = new long[jobs.length];
        PriorityQueue<TimeWorkerPair> workerPq = new PriorityQueue<>();
        for (int i = 0; i < numWorkers; i++) {
            workerPq.add(new TimeWorkerPair(0, i));
        }
        for (int i = 0; i < jobs.length; i++) {
            TimeWorkerPair l = workerPq.remove();
            startTime[i] = l.timerTally;
            assignedWorker[i] = l.workerID;
            workerPq.add(new TimeWorkerPair(l.timerTally + jobs[i], l.workerID));
        }
    }

    private void solve() throws IOException {
        in = new FastScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        readData();
        assignJobsImproved();
        writeResponse(assignedWorker, startTime);
        out.close();
    }

    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        String next() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}