-
Notifications
You must be signed in to change notification settings - Fork 0
/
warrior chef
60 lines (48 loc) · 1.21 KB
/
warrior chef
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canDefeatAllEnemies(int n, int h, const vector<int>& strengths, int resistance) {
for (int enemyStrength : strengths) {
if (enemyStrength <= resistance) {
continue;
} else if (h > enemyStrength) {
h -= enemyStrength;
} else {
return false;
}
}
return true;
}
int findMinResistance(int n, int h, const vector<int>& strengths) {
int low = 0;
int high = *max_element(strengths.begin(), strengths.end());
while (low < high) {
int mid = (low + high) / 2;
if (canDefeatAllEnemies(n, h, strengths, mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
int main() {
int t;
cin >> t;
vector<int> results;
for (int i = 0; i < t; ++i) {
int n, h;
cin >> n >> h;
vector<int> strengths(n);
for (int j = 0; j < n; ++j) {
cin >> strengths[j];
}
int result = findMinResistance(n, h, strengths);
results.push_back(result);
}
for (int result : results) {
cout << result << endl;
}
return 0;
}