-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedulerRedux.cpp
More file actions
85 lines (78 loc) · 1.88 KB
/
SchedulerRedux.cpp
File metadata and controls
85 lines (78 loc) · 1.88 KB
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
// Problem link: https://csacademy.com/ieeextreme-practice/task/scheduler-redux
// By: Osama Khallouf
#include <bits/stdc++.h>
using namespace std;
long long M = 1'000'000'007;
long long pwr(long long x, long long y) { // Calculates X to the power Y
if (y == 0) return 1;
long long half = pwr(x, y/2);
half *= half; // full power (- 1)
half %= M;
if(y % 2 == 1) {
return (half * x) % M;
}
else {
return half;
}
}
vector<long long> workers[21];
vector<long long> jobs;
bool cmp(vector<long long> &w1, vector<long long> &w2) {
// return true when w1 < w2
for (int i = 0; i < w1.size() && i < w2.size() ; i++){
if(w1[i] != w2[i]) {
return w1[i] < w2[i];
}
}
return w1.size() < w2.size();
}
// 8, 7, 5, 2
// 8, 7, 5
void push(int idx, long long job) {
if(workers[idx].empty()){
workers[idx].push_back(job);
return;
}
while (!workers[idx].empty() && workers[idx].back() == job) {
workers[idx].pop_back();
job ++;
}
workers[idx].push_back(job);
}
int main() {
int n,m;
cin >> n >> m;
jobs.resize(n);
for(int i =0 ; i < n; i ++) {
cin >> jobs[i];
}
if(m == 1) {
long long ans = 0;
for (long long job : jobs) {
ans += pwr(2, job);
ans %= M;
}
cout << ans;
return 0;
}
sort(jobs.rbegin(), jobs.rend());
int cur = 0, maximum = 0;
workers[0].push_back(jobs[0]);
cur = 1;
for(int i = 1; i < n ; i ++) {
// workers[cur].push_back(jobs[i]);
push(cur, jobs[i]);
if (!cmp(workers[cur], workers[maximum])) {
maximum = cur;
cur += 1;
cur %= m;
}
}
long long ans = 0;
for (long long job : workers[maximum]) {
ans += pwr(2, job);
ans %= M;
}
cout << ans;
return 0;
}