-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathB.cpp
More file actions
executable file
·51 lines (43 loc) · 944 Bytes
/
B.cpp
File metadata and controls
executable file
·51 lines (43 loc) · 944 Bytes
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
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <functional>
using namespace std;
string subtransmutation(int N, int A, int B, vector<int>& U) {
int metal = N, limit = 1000;
// helper function
auto good = [&](int x) {
vector<int> cnt(x + 1, 0);
cnt[x] = 1;
for (int i = x; i > 0; i--) {
cnt[i] = min(cnt[i], limit);
if (i <= N)
cnt[i] -= U[i];
if (cnt[i] < 0)
return false;
if (i > A)
cnt[i - A] += cnt[i];
if (i > B)
cnt[i - B] += cnt[i];
}
return true;
};
// binary search
while (!good(metal) && metal < limit)
metal++;
return (metal == limit) ? "IMPOSSIBLE" : to_string(metal);
}
int main() {
int T;
cin >> T;
for (int x = 1; x <= T; x++) {
int N, A, B;
cin >> N >> A >> B;
vector<int> U(N + 1);
for (int i = 1; i <= N; i++)
cin >> U[i];
cout << "Case #" << x << ": " << subtransmutation(N, A, B, U) << endl;
}
return 0;
}