-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path2973.cpp
62 lines (52 loc) · 1.24 KB
/
2973.cpp
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
/*
miscellaneous > binary search > on answer
difficulty: medium
date: 03/Jan/2020
by: @brpapa
*/
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int N, C, T;
vector<int> qtyPopcorns; // qtyPopcorns[i] = qte de pipocas do saco i
// não mais que C competidores conseguem comer tudo em x segundos?
bool can(int x) {
int qtyCanEat = x * T; // máximo de pipocas que o competidor atual pode comer
int c = 0; // competidor atual
for (int qty : qtyPopcorns) {
if (qtyCanEat >= qty) {
qtyCanEat -= qty; // c comeu
} else {
c++;
qtyCanEat = x * T - qty; // próximo c comeu
if (qtyCanEat < 0 || c == C)
return false;
}
}
return true;
}
int main() {
cin >> N >> C >> T;
int low = 0, high = 0;
qtyPopcorns.assign(N, 0);
int timeNeeded;
for (int &qty : qtyPopcorns) {
cin >> qty;
timeNeeded = ceil((double)qty / T);
low = max(low, timeNeeded);
high += timeNeeded;
}
int ans;
while (low <= high) {
int mid = (low + high) / 2;
if (can(mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << ans << endl;
return 0;
}