output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> int h[15]; int an[15]; int y[15]; int n, a, b; int ans = (1 << 30); void dfs(int x, int now) { if (now >= ans) { return; } if (x == n) { if (h[x] < 0) { ans = now; for (int i = 1; i <= n; i++) { an[i] = y[i]; } } return; } if (h[x - 1] < 0) { dfs(x + 1, now); } int tmp = 0; for (int i = 1; h[x - 1] >= 0 || h[x] >= 0 || h[x + 1] >= 0; i++) { tmp++; h[x - 1] -= b; h[x] -= a; h[x + 1] -= b; y[x] = tmp; if (h[x - 1] < 0) { dfs(x + 1, now + tmp); } } y[x] = 0; h[x - 1] += b * tmp; h[x] += a * tmp; h[x + 1] += b * tmp; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); } dfs(2, 0); printf("%d\n", ans); for (int i = 1; i <= n; i++) { while (an[i]--) { printf("%d ", i); } } puts(""); return 0; }
### Prompt Create a solution in cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> int h[15]; int an[15]; int y[15]; int n, a, b; int ans = (1 << 30); void dfs(int x, int now) { if (now >= ans) { return; } if (x == n) { if (h[x] < 0) { ans = now; for (int i = 1; i <= n; i++) { an[i] = y[i]; } } return; } if (h[x - 1] < 0) { dfs(x + 1, now); } int tmp = 0; for (int i = 1; h[x - 1] >= 0 || h[x] >= 0 || h[x + 1] >= 0; i++) { tmp++; h[x - 1] -= b; h[x] -= a; h[x + 1] -= b; y[x] = tmp; if (h[x - 1] < 0) { dfs(x + 1, now + tmp); } } y[x] = 0; h[x - 1] += b * tmp; h[x] += a * tmp; h[x + 1] += b * tmp; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); } dfs(2, 0); printf("%d\n", ans); for (int i = 1; i <= n; i++) { while (an[i]--) { printf("%d ", i); } } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; int nu[12], n, a, b, ans = 0x3f3f3f3f, out[12], out1[12]; void dfs(int x, int c) { if (c >= ans) return; if (x == n) { ans = c; for (int i = 1; i <= n; i++) out1[i] = out[i]; } for (int i = 0;; i++) { if (nu[x - 1] - i * b >= 0) continue; if (i + c >= ans) break; if (x == n - 1 && nu[x + 1] - b * i >= 0) continue; out[x] = i; nu[x - 1] -= b * i; nu[x] -= a * i; nu[x + 1] -= b * i; dfs(x + 1, c + i); nu[x - 1] += b * i; nu[x] += a * i; nu[x + 1] += b * i; if (nu[x] - a * i < 0 && nu[x + 1] - b * i < 0) break; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &nu[i]); dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) for (int j = 0; j < out1[i]; j++) printf("%d ", i); }
### Prompt Develop a solution in CPP to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int nu[12], n, a, b, ans = 0x3f3f3f3f, out[12], out1[12]; void dfs(int x, int c) { if (c >= ans) return; if (x == n) { ans = c; for (int i = 1; i <= n; i++) out1[i] = out[i]; } for (int i = 0;; i++) { if (nu[x - 1] - i * b >= 0) continue; if (i + c >= ans) break; if (x == n - 1 && nu[x + 1] - b * i >= 0) continue; out[x] = i; nu[x - 1] -= b * i; nu[x] -= a * i; nu[x + 1] -= b * i; dfs(x + 1, c + i); nu[x - 1] += b * i; nu[x] += a * i; nu[x + 1] += b * i; if (nu[x] - a * i < 0 && nu[x + 1] - b * i < 0) break; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &nu[i]); dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) for (int j = 0; j < out1[i]; j++) printf("%d ", i); } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; int n, a, b; vector<int> h; vector<int> hits; vector<int> ans; void go(int i) { if (i == n) { if (accumulate(begin(hits), end(hits), 0) < accumulate(begin(ans), end(ans), 0)) { ans = hits; } return; } int mn = max(0, (h[i - 1] - hits[i - 1] * a - hits[i - 2] * b + b - 1) / b); int mx = max(mn, (h[i] - hits[i - 1] * b + a - 1) / a); if (i == n - 1) { mn = max(mn, (h[n] + b - 1) / b); mx = max(mx, mn); } for (hits[i] = mn; hits[i] <= mx; ++hits[i]) { go(i + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b; h.resize(n + 1); hits.resize(n + 1); ans = vector<int>(n + 1, 100); for (int i = 1; i <= n; ++i) { cin >> h[i]; ++h[i]; } go(2); cout << accumulate(begin(ans), end(ans), 0) << '\n'; for (int i = 1; i <= n; ++i) { for (int j = 0; j < ans[i]; ++j) { cout << i << ' '; } } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; int n, a, b; vector<int> h; vector<int> hits; vector<int> ans; void go(int i) { if (i == n) { if (accumulate(begin(hits), end(hits), 0) < accumulate(begin(ans), end(ans), 0)) { ans = hits; } return; } int mn = max(0, (h[i - 1] - hits[i - 1] * a - hits[i - 2] * b + b - 1) / b); int mx = max(mn, (h[i] - hits[i - 1] * b + a - 1) / a); if (i == n - 1) { mn = max(mn, (h[n] + b - 1) / b); mx = max(mx, mn); } for (hits[i] = mn; hits[i] <= mx; ++hits[i]) { go(i + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b; h.resize(n + 1); hits.resize(n + 1); ans = vector<int>(n + 1, 100); for (int i = 1; i <= n; ++i) { cin >> h[i]; ++h[i]; } go(2); cout << accumulate(begin(ans), end(ans), 0) << '\n'; for (int i = 1; i <= n; ++i) { for (int j = 0; j < ans[i]; ++j) { cout << i << ' '; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1E6; int N; vector<int> arc; int a, b; int dp[17][17][12]; int minATK(int pre, int cur, int i) { int min_atk = INF, low = (pre + b - 1) / b, up = max((pre + b - 1) / b, (cur + a - 1) / a); if (i == N - 2) { min_atk = max(up, (arc[N - 1] + b - 1) / b); dp[pre][cur][i] = min_atk; return min_atk; } for (int iatk = up; iatk >= low; iatk--) { int tmp_pre = max(cur - iatk * a, 0), tmp_cur = max(arc[i + 1] - iatk * b, 0); int tmp = minATK(tmp_pre, tmp_cur, i + 1) + iatk; if (tmp < min_atk) { min_atk = tmp; dp[pre][cur][i] = iatk; } } return min_atk; } int main() { cin >> N >> a >> b; for (int i = 0; i < N; i++) { int hp; scanf("%d", &hp); arc.push_back(hp + 1); } memset(dp, 0, sizeof(dp)); int min_atk = minATK(arc[0], arc[1], 1); printf("%d\n", min_atk); int pre = arc[0], cur = arc[1]; for (int i = 1; i <= N - 1; i++) { int k = dp[pre][cur][i]; for (int j = 0; j < k; j++) { printf("%d ", i + 1); } pre = max(cur - k * a, 0); cur = max(arc[i + 1] - k * b, 0); } }
### Prompt Please create a solution in CPP to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1E6; int N; vector<int> arc; int a, b; int dp[17][17][12]; int minATK(int pre, int cur, int i) { int min_atk = INF, low = (pre + b - 1) / b, up = max((pre + b - 1) / b, (cur + a - 1) / a); if (i == N - 2) { min_atk = max(up, (arc[N - 1] + b - 1) / b); dp[pre][cur][i] = min_atk; return min_atk; } for (int iatk = up; iatk >= low; iatk--) { int tmp_pre = max(cur - iatk * a, 0), tmp_cur = max(arc[i + 1] - iatk * b, 0); int tmp = minATK(tmp_pre, tmp_cur, i + 1) + iatk; if (tmp < min_atk) { min_atk = tmp; dp[pre][cur][i] = iatk; } } return min_atk; } int main() { cin >> N >> a >> b; for (int i = 0; i < N; i++) { int hp; scanf("%d", &hp); arc.push_back(hp + 1); } memset(dp, 0, sizeof(dp)); int min_atk = minATK(arc[0], arc[1], 1); printf("%d\n", min_atk); int pre = arc[0], cur = arc[1]; for (int i = 1; i <= N - 1; i++) { int k = dp[pre][cur][i]; for (int j = 0; j < k; j++) { printf("%d ", i + 1); } pre = max(cur - k * a, 0); cur = max(arc[i + 1] - k * b, 0); } } ```
#include <bits/stdc++.h> using namespace std; int a, b, n; map<long long, pair<int, long long>> dps[10]; inline int get(long long h, int idx) { int result = (h >> (6 * idx)) & 63ll; if (result >= 32) { return -1; } else { return result; } } inline long long put(long long h, int idx, long long val) { h &= ~(63ll << (6 * idx)); if (val < 0ll) { h |= 63ll << (6 * idx); } else { h |= val << (6 * idx); } return h; } pair<int, long long> brute(long long h, int idx) { long long save = h; if (idx == n - 2) { int mx = max(max(0, get(h, n - 3) / b + 1), max(get(h, n - 2) / a + 1, get(h, n - 1) / b + 1)); return make_pair(mx, put(0ll, idx, mx)); } int result = 0; int hp = get(h, idx - 1); if (hp >= 0) { result += hp / b + 1; h = put(h, idx - 1, -1); h = put(h, idx, get(save, idx) - result * a); h = put(h, idx + 1, get(save, idx + 1) - result * b); } pair<int, long long> tmpr; int ltmp = get(h, idx - 1); int ctmp = get(h, idx); int rtmp = get(h, idx + 1); if (ctmp < 0) { tmpr = brute(h, idx + 1); return make_pair(result + tmpr.first, put(tmpr.second, idx, result)); } auto it = dps[idx].find(save); if (it != dps[idx].end()) { return it->second; } int turns = ctmp / a + 1; tmpr = brute(put(put(h, idx, -1), idx + 1, rtmp - turns * b), idx + 1); int minv = turns + tmpr.first; long long minp = put(tmpr.second, idx, result + turns); for (int i = 0; i < turns; i++) { tmpr = brute(put(put(h, idx, ctmp - i * a), idx + 1, rtmp - i * b), idx + 1); int lv = i + tmpr.first; if (lv < minv) { minv = lv; minp = put(tmpr.second, idx, result + i); } } pair<int, long long> ans = make_pair(minv + result, minp); dps[idx].insert(make_pair(save, ans)); return ans; } int main() { ios_base::sync_with_stdio(false); cin >> n >> a >> b; long long h = 0; long long pow = 1; for (int i = 0; i < n; i++) { int p; cin >> p; h += p * pow; pow *= 64; } pair<int, long long> result = brute(h, 1); cout << result.first << '\n'; for (int i = 0; i < n; i++) { int v = get(result.second, i); for (int j = 0; j < v; j++) { cout << i + 1 << ' '; } } cout << '\n'; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b, n; map<long long, pair<int, long long>> dps[10]; inline int get(long long h, int idx) { int result = (h >> (6 * idx)) & 63ll; if (result >= 32) { return -1; } else { return result; } } inline long long put(long long h, int idx, long long val) { h &= ~(63ll << (6 * idx)); if (val < 0ll) { h |= 63ll << (6 * idx); } else { h |= val << (6 * idx); } return h; } pair<int, long long> brute(long long h, int idx) { long long save = h; if (idx == n - 2) { int mx = max(max(0, get(h, n - 3) / b + 1), max(get(h, n - 2) / a + 1, get(h, n - 1) / b + 1)); return make_pair(mx, put(0ll, idx, mx)); } int result = 0; int hp = get(h, idx - 1); if (hp >= 0) { result += hp / b + 1; h = put(h, idx - 1, -1); h = put(h, idx, get(save, idx) - result * a); h = put(h, idx + 1, get(save, idx + 1) - result * b); } pair<int, long long> tmpr; int ltmp = get(h, idx - 1); int ctmp = get(h, idx); int rtmp = get(h, idx + 1); if (ctmp < 0) { tmpr = brute(h, idx + 1); return make_pair(result + tmpr.first, put(tmpr.second, idx, result)); } auto it = dps[idx].find(save); if (it != dps[idx].end()) { return it->second; } int turns = ctmp / a + 1; tmpr = brute(put(put(h, idx, -1), idx + 1, rtmp - turns * b), idx + 1); int minv = turns + tmpr.first; long long minp = put(tmpr.second, idx, result + turns); for (int i = 0; i < turns; i++) { tmpr = brute(put(put(h, idx, ctmp - i * a), idx + 1, rtmp - i * b), idx + 1); int lv = i + tmpr.first; if (lv < minv) { minv = lv; minp = put(tmpr.second, idx, result + i); } } pair<int, long long> ans = make_pair(minv + result, minp); dps[idx].insert(make_pair(save, ans)); return ans; } int main() { ios_base::sync_with_stdio(false); cin >> n >> a >> b; long long h = 0; long long pow = 1; for (int i = 0; i < n; i++) { int p; cin >> p; h += p * pow; pow *= 64; } pair<int, long long> result = brute(h, 1); cout << result.first << '\n'; for (int i = 0; i < n; i++) { int v = get(result.second, i); for (int j = 0; j < v; j++) { cout << i + 1 << ' '; } } cout << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int h[20], n, a, b, dp[500][20][500]; int ans(int prv, int pos, int cur) { if (dp[prv + 250][pos][cur + 250] != -1) return dp[prv + 250][pos][cur + 250]; if (pos == n) { if (cur < 0) return dp[prv + 250][pos][cur + 250] = 0; else return dp[prv + 250][pos][cur + 250] = 10000000; } int y = 10000000, nxt = h[pos + 1]; for (int i = 0;; ++i) { if (prv - i * b < 0) { y = min(y, i + ans(cur - i * a, pos + 1, nxt - i * b)); } if (nxt - i * b < 0 && prv - i * b < 0 && cur - i * a < 0) break; } return dp[prv + 250][pos][cur + 250] = y; } void printans(int prv, int pos, int cur) { if (pos == n) { return; } int y = ans(prv, pos, cur), nxt = h[pos + 1]; for (int i = 0;; ++i) { if (prv - i * b < 0) { if (y == i + ans(cur - i * a, pos + 1, nxt - i * b)) { for (int j = 0; j < i; ++j) { printf("%d ", pos); } printans(cur - i * a, pos + 1, nxt - i * b); return; } } if (nxt - i * b < 0 && prv - i * b < 0 && cur - i * a < 0) break; } } int main() { scanf("%d", &n); scanf("%d", &a); scanf("%d", &b); memset(h, -1, sizeof(h)); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); } memset(dp, -1, sizeof(dp)); printf("%d\n", ans(h[1], 2, h[2])); printans(h[1], 2, h[2]); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int h[20], n, a, b, dp[500][20][500]; int ans(int prv, int pos, int cur) { if (dp[prv + 250][pos][cur + 250] != -1) return dp[prv + 250][pos][cur + 250]; if (pos == n) { if (cur < 0) return dp[prv + 250][pos][cur + 250] = 0; else return dp[prv + 250][pos][cur + 250] = 10000000; } int y = 10000000, nxt = h[pos + 1]; for (int i = 0;; ++i) { if (prv - i * b < 0) { y = min(y, i + ans(cur - i * a, pos + 1, nxt - i * b)); } if (nxt - i * b < 0 && prv - i * b < 0 && cur - i * a < 0) break; } return dp[prv + 250][pos][cur + 250] = y; } void printans(int prv, int pos, int cur) { if (pos == n) { return; } int y = ans(prv, pos, cur), nxt = h[pos + 1]; for (int i = 0;; ++i) { if (prv - i * b < 0) { if (y == i + ans(cur - i * a, pos + 1, nxt - i * b)) { for (int j = 0; j < i; ++j) { printf("%d ", pos); } printans(cur - i * a, pos + 1, nxt - i * b); return; } } if (nxt - i * b < 0 && prv - i * b < 0 && cur - i * a < 0) break; } } int main() { scanf("%d", &n); scanf("%d", &a); scanf("%d", &b); memset(h, -1, sizeof(h)); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); } memset(dp, -1, sizeof(dp)); printf("%d\n", ans(h[1], 2, h[2])); printans(h[1], 2, h[2]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a, b; int h[110]; vector<int> G, ans; void dfs(int u, bool hehe) { if (u == n && hehe == false) { if (G.size() < ans.size()) ans = G; return; } int siz = 0; if (hehe) { while (h[u - 1] > 0) siz++, h[u - 1] -= b, h[u] -= a, h[u + 1] -= b, G.push_back(u); } if (h[u] > 0) { while (h[u] > 0) { dfs(u + 1, true); h[u - 1] -= b; h[u] -= a; h[u + 1] -= b; siz++; G.push_back(u); } } dfs(u + 1, false); h[u] += a * siz; h[u + 1] += b * siz; h[u - 1] += b * siz; while (siz--) G.erase(G.end() - 1); } int main() { int i, j; while (~scanf("%d %d %d", &n, &a, &b)) { G.clear(); ans.clear(); for (i = 1; i <= n; i++) scanf("%d", &h[i]), h[i]++; while (h[1] > 0) { h[2] -= a; h[1] -= b; h[3] -= b; ans.push_back(2); } while (h[n] > 0) { h[n - 2] -= b; h[n - 1] -= a; h[n] -= b; ans.push_back(n - 1); } G = ans; for (i = 1; i <= 100; i++) ans.push_back(i); dfs(2, false); printf("%d\n", ans.size()); for (i = 0; i < ans.size(); i++) printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' '); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b; int h[110]; vector<int> G, ans; void dfs(int u, bool hehe) { if (u == n && hehe == false) { if (G.size() < ans.size()) ans = G; return; } int siz = 0; if (hehe) { while (h[u - 1] > 0) siz++, h[u - 1] -= b, h[u] -= a, h[u + 1] -= b, G.push_back(u); } if (h[u] > 0) { while (h[u] > 0) { dfs(u + 1, true); h[u - 1] -= b; h[u] -= a; h[u + 1] -= b; siz++; G.push_back(u); } } dfs(u + 1, false); h[u] += a * siz; h[u + 1] += b * siz; h[u - 1] += b * siz; while (siz--) G.erase(G.end() - 1); } int main() { int i, j; while (~scanf("%d %d %d", &n, &a, &b)) { G.clear(); ans.clear(); for (i = 1; i <= n; i++) scanf("%d", &h[i]), h[i]++; while (h[1] > 0) { h[2] -= a; h[1] -= b; h[3] -= b; ans.push_back(2); } while (h[n] > 0) { h[n - 2] -= b; h[n - 1] -= a; h[n] -= b; ans.push_back(n - 1); } G = ans; for (i = 1; i <= 100; i++) ans.push_back(i); dfs(2, false); printf("%d\n", ans.size()); for (i = 0; i < ans.size(); i++) printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' '); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int KILLED = 19; int n, a, b, h[15], dp[15][20][20]; int F(int i, int last, int sub) { if (i == n) { int got = h[i] - sub * b; if (last == KILLED && got < 0) return 0; return INF; } int &ans = dp[i][last][sub]; if (ans != -1) return ans; ans = INF; int got = h[i] - sub * b; for (int k = 0; k < KILLED; k++) { if (last != KILLED && last - k * b >= 0) continue; int v = 0; if (got - k * a < 0) v = F(i + 1, KILLED, k); else v = F(i + 1, got - k * a, k); if (v != INF) ans = min(ans, v + k); } return ans; } vector<int> ans; void dfs(int i, int last, int sub) { if (i == n) return; int got = h[i] - sub * b; for (int k = 0; k < KILLED; k++) { if (last != KILLED && last - k * b >= 0) continue; if (got - k * a < 0) { int v = F(i + 1, KILLED, k); if (v != INF && v + k == F(i, last, sub)) { for (int q = 1; q <= k; q++) ans.push_back(i); dfs(i + 1, KILLED, k); return; } } else { int v = F(i + 1, got - k * a, k); if (v != INF && v + k == F(i, last, sub)) { for (int q = 1; q <= k; q++) ans.push_back(i); dfs(i + 1, got - k * a, k); return; } } } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); memset(dp, -1, sizeof(dp)); printf("%d\n", F(2, h[1], 0)); dfs(2, h[1], 0); for (int o : ans) printf("%d ", o); printf("\n"); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9; int KILLED = 19; int n, a, b, h[15], dp[15][20][20]; int F(int i, int last, int sub) { if (i == n) { int got = h[i] - sub * b; if (last == KILLED && got < 0) return 0; return INF; } int &ans = dp[i][last][sub]; if (ans != -1) return ans; ans = INF; int got = h[i] - sub * b; for (int k = 0; k < KILLED; k++) { if (last != KILLED && last - k * b >= 0) continue; int v = 0; if (got - k * a < 0) v = F(i + 1, KILLED, k); else v = F(i + 1, got - k * a, k); if (v != INF) ans = min(ans, v + k); } return ans; } vector<int> ans; void dfs(int i, int last, int sub) { if (i == n) return; int got = h[i] - sub * b; for (int k = 0; k < KILLED; k++) { if (last != KILLED && last - k * b >= 0) continue; if (got - k * a < 0) { int v = F(i + 1, KILLED, k); if (v != INF && v + k == F(i, last, sub)) { for (int q = 1; q <= k; q++) ans.push_back(i); dfs(i + 1, KILLED, k); return; } } else { int v = F(i + 1, got - k * a, k); if (v != INF && v + k == F(i, last, sub)) { for (int q = 1; q <= k; q++) ans.push_back(i); dfs(i + 1, got - k * a, k); return; } } } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); memset(dp, -1, sizeof(dp)); printf("%d\n", F(2, h[1], 0)); dfs(2, h[1], 0); for (int o : ans) printf("%d ", o); printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; map<vector<int>, int> f; map<vector<int>, int> g; int n, a, b; vector<int> beg; int tot = 0; int dfs(vector<int> v) { if (f.find(v) != f.end()) return f[v]; if (*max_element((v).begin(), (v).end()) < 0) return f[v] = 0; f[v] = 1000000000; for (int i = 1; i < (int)v.size() - 1; ++i) if (v[i] >= 0 || v[i - 1] >= 0 || v[i + 1] >= 0) { vector<int> nv = v; nv[i] -= a; if (nv[i] < 0) nv[i] = -1; nv[i - 1] -= b; if (nv[i - 1] < 0) nv[i - 1] = -1; nv[i + 1] -= b; if (nv[i + 1] < 0) nv[i + 1] = -1; int val = dfs(nv); if (f[v] > val + 1) { f[v] = val + 1; g[v] = i; } } return f[v]; } int main() { cin >> n >> a >> b; beg.resize(n); for (int(i) = 0; (i) < (n); ++(i)) cin >> beg[i]; vector<int> plus; plus.clear(); int ans = 0; while (beg[0] >= 0) { ++ans; plus.push_back(2); beg[0] -= b; if (beg[0] < 0) beg[0] = -1; beg[1] -= a; if (beg[1] < 0) beg[1] = -1; beg[2] -= b; if (beg[2] < 0) beg[2] = -1; } while (beg.back() >= 0) { ++ans; plus.push_back((int)beg.size() - 1); beg.back() -= b; if (beg.back() < 0) beg.back() = -1; beg[(int)beg.size() - 2] -= a; if (beg[(int)beg.size() - 2] < 0) beg[(int)beg.size() - 2] = -1; beg[(int)beg.size() - 3] -= b; if (beg[(int)beg.size() - 3] < 0) beg[(int)beg.size() - 3] = -1; } cout << ans + dfs(beg) << endl; for (int(i) = 0; (i) < ((int)plus.size()); ++(i)) cout << plus[i] << " "; while (*max_element((beg).begin(), (beg).end()) >= 0) { int pos = g[beg]; cout << pos + 1 << " "; beg[pos] -= a; if (beg[pos] < 0) beg[pos] = -1; beg[pos - 1] -= b; if (beg[pos - 1] < 0) beg[pos - 1] = -1; beg[pos + 1] -= b; if (beg[pos + 1] < 0) beg[pos + 1] = -1; } cout << endl; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<vector<int>, int> f; map<vector<int>, int> g; int n, a, b; vector<int> beg; int tot = 0; int dfs(vector<int> v) { if (f.find(v) != f.end()) return f[v]; if (*max_element((v).begin(), (v).end()) < 0) return f[v] = 0; f[v] = 1000000000; for (int i = 1; i < (int)v.size() - 1; ++i) if (v[i] >= 0 || v[i - 1] >= 0 || v[i + 1] >= 0) { vector<int> nv = v; nv[i] -= a; if (nv[i] < 0) nv[i] = -1; nv[i - 1] -= b; if (nv[i - 1] < 0) nv[i - 1] = -1; nv[i + 1] -= b; if (nv[i + 1] < 0) nv[i + 1] = -1; int val = dfs(nv); if (f[v] > val + 1) { f[v] = val + 1; g[v] = i; } } return f[v]; } int main() { cin >> n >> a >> b; beg.resize(n); for (int(i) = 0; (i) < (n); ++(i)) cin >> beg[i]; vector<int> plus; plus.clear(); int ans = 0; while (beg[0] >= 0) { ++ans; plus.push_back(2); beg[0] -= b; if (beg[0] < 0) beg[0] = -1; beg[1] -= a; if (beg[1] < 0) beg[1] = -1; beg[2] -= b; if (beg[2] < 0) beg[2] = -1; } while (beg.back() >= 0) { ++ans; plus.push_back((int)beg.size() - 1); beg.back() -= b; if (beg.back() < 0) beg.back() = -1; beg[(int)beg.size() - 2] -= a; if (beg[(int)beg.size() - 2] < 0) beg[(int)beg.size() - 2] = -1; beg[(int)beg.size() - 3] -= b; if (beg[(int)beg.size() - 3] < 0) beg[(int)beg.size() - 3] = -1; } cout << ans + dfs(beg) << endl; for (int(i) = 0; (i) < ((int)plus.size()); ++(i)) cout << plus[i] << " "; while (*max_element((beg).begin(), (beg).end()) >= 0) { int pos = g[beg]; cout << pos + 1 << " "; beg[pos] -= a; if (beg[pos] < 0) beg[pos] = -1; beg[pos - 1] -= b; if (beg[pos - 1] < 0) beg[pos - 1] = -1; beg[pos + 1] -= b; if (beg[pos + 1] < 0) beg[pos + 1] = -1; } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 0x3f3f3f3f; struct Node { int h[10]; int c; int g; int f; vector<int> ans; bool operator<(const Node &r) const { return f < r.f; } bool operator>(const Node &r) const { return f > r.f; } }; int getw(Node &node, int x, int n) { int res = 0; for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { res += node.h[i] + 1; } } if (res % x == 0) { node.g = res / x; return res / x; } else { node.g = res / x + 1; return res / x + 1; } } bool reached(Node node, int n) { for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { return false; } } return true; } void shot(int pos, int a, int b, Node &node, int n) { node.h[pos] -= a; if (pos - 1 >= 0) { node.h[pos - 1] -= b; } if (pos + 1 < n) { node.h[pos + 1] -= b; } } int getpos(Node &node, int n) { for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { return i; } } return -1; } int main() { int n, a, b; cin >> n >> a >> b; vector<int> h(n, 0); for (int i = 0; i < n; ++i) { cin >> h[i]; } vector<int> ans; Node node; for (int i = 0; i < h.size(); ++i) { node.h[i] = h[i]; } getw(node, a + b + b, n); node.c = 0; node.f = node.c + node.g; while (node.h[0] >= 0) { shot(1, a, b, node, n); node.ans.push_back(2); } while (node.h[n - 1] >= 0) { shot(n - 2, a, b, node, n); node.ans.push_back(n - 1); } priority_queue<Node, vector<Node>, greater<Node> > T; while (!T.empty()) T.pop(); T.push(node); while (!T.empty()) { node = T.top(); T.pop(); if (reached(node, h.size())) { break; } else { Node n1, n2; n1.c = n2.c = node.c + 1; for (int i = 0; i < h.size(); ++i) { n1.h[i] = n2.h[i] = node.h[i]; } int pos = getpos(node, (int)h.size()); shot(pos, a, b, n1, h.size()); getw(n1, a + b + b, (int)h.size()); n1.f = n1.c + n1.g; n1.ans.assign(node.ans.begin(), node.ans.end()); n1.ans.push_back(pos + 1); T.push(n1); if (pos < n - 1) { shot(pos + 1, a, b, n2, h.size()); getw(n2, a + b + b, (int)h.size()); n2.f = n2.c + n2.g; n2.ans.assign(node.ans.begin(), node.ans.end()); n2.ans.push_back(pos + 2); T.push(n2); } } } cout << node.ans.size() << endl; for (int i = 0; i < node.ans.size(); ++i) { cout << node.ans[i]; if (i == node.ans.size() - 1) { cout << endl; } else { cout << ' '; } } }
### Prompt Please provide a CPP coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 0x3f3f3f3f; struct Node { int h[10]; int c; int g; int f; vector<int> ans; bool operator<(const Node &r) const { return f < r.f; } bool operator>(const Node &r) const { return f > r.f; } }; int getw(Node &node, int x, int n) { int res = 0; for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { res += node.h[i] + 1; } } if (res % x == 0) { node.g = res / x; return res / x; } else { node.g = res / x + 1; return res / x + 1; } } bool reached(Node node, int n) { for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { return false; } } return true; } void shot(int pos, int a, int b, Node &node, int n) { node.h[pos] -= a; if (pos - 1 >= 0) { node.h[pos - 1] -= b; } if (pos + 1 < n) { node.h[pos + 1] -= b; } } int getpos(Node &node, int n) { for (int i = 0; i < n; ++i) { if (node.h[i] >= 0) { return i; } } return -1; } int main() { int n, a, b; cin >> n >> a >> b; vector<int> h(n, 0); for (int i = 0; i < n; ++i) { cin >> h[i]; } vector<int> ans; Node node; for (int i = 0; i < h.size(); ++i) { node.h[i] = h[i]; } getw(node, a + b + b, n); node.c = 0; node.f = node.c + node.g; while (node.h[0] >= 0) { shot(1, a, b, node, n); node.ans.push_back(2); } while (node.h[n - 1] >= 0) { shot(n - 2, a, b, node, n); node.ans.push_back(n - 1); } priority_queue<Node, vector<Node>, greater<Node> > T; while (!T.empty()) T.pop(); T.push(node); while (!T.empty()) { node = T.top(); T.pop(); if (reached(node, h.size())) { break; } else { Node n1, n2; n1.c = n2.c = node.c + 1; for (int i = 0; i < h.size(); ++i) { n1.h[i] = n2.h[i] = node.h[i]; } int pos = getpos(node, (int)h.size()); shot(pos, a, b, n1, h.size()); getw(n1, a + b + b, (int)h.size()); n1.f = n1.c + n1.g; n1.ans.assign(node.ans.begin(), node.ans.end()); n1.ans.push_back(pos + 1); T.push(n1); if (pos < n - 1) { shot(pos + 1, a, b, n2, h.size()); getw(n2, a + b + b, (int)h.size()); n2.f = n2.c + n2.g; n2.ans.assign(node.ans.begin(), node.ans.end()); n2.ans.push_back(pos + 2); T.push(n2); } } } cout << node.ans.size() << endl; for (int i = 0; i < node.ans.size(); ++i) { cout << node.ans[i]; if (i == node.ans.size() - 1) { cout << endl; } else { cout << ' '; } } } ```
#include <bits/stdc++.h> int Max(int X, int Y) { return X > Y ? X : Y; } int Min(int X, int Y) { return X < Y ? X : Y; } int EnemyNum; int DirectDamage, BlastDamage; int const MaxEnemyCount = 12; int EnemyHealth[MaxEnemyCount]; int const MaxHitNum = 100000; int MinHitNum = MaxHitNum; int MinHit[MaxHitNum]; void Search(int EnemyIndex, int Hit[], int HitNum) { if (EnemyIndex > EnemyNum - 2) { if (HitNum < MinHitNum) { MinHitNum = HitNum; memcpy(MinHit, Hit, sizeof(int) * HitNum); } } else if (EnemyIndex == EnemyNum - 2) { int DirectHitNum = (Max(EnemyHealth[EnemyIndex], 0) + DirectDamage - 1) / DirectDamage; for (int I = 0; I < DirectHitNum; I++) { Hit[HitNum + I] = EnemyIndex; } Search(EnemyIndex + 1, Hit, HitNum + DirectHitNum); } else { int PossibleHitNum = (Max(EnemyHealth[EnemyIndex], 0) + DirectDamage - 1) / DirectDamage; for (int DirectHitNum = 0; DirectHitNum <= PossibleHitNum; DirectHitNum++) { int BlastHitNum = (Max(EnemyHealth[EnemyIndex] - DirectHitNum * DirectDamage, 0) + BlastDamage - 1) / BlastDamage; EnemyHealth[EnemyIndex + 1] -= DirectHitNum * BlastDamage + BlastHitNum * DirectDamage; EnemyHealth[EnemyIndex + 2] -= BlastHitNum * BlastDamage; for (int I = 0; I < DirectHitNum; I++) { Hit[HitNum + I] = EnemyIndex; } for (int I = 0; I < BlastHitNum; I++) { Hit[HitNum + DirectHitNum + I] = EnemyIndex + 1; } Search(EnemyIndex + 1, Hit, HitNum + DirectHitNum + BlastHitNum); EnemyHealth[EnemyIndex + 1] += DirectHitNum * BlastDamage + BlastHitNum * DirectDamage; EnemyHealth[EnemyIndex + 2] += BlastHitNum * BlastDamage; } } } int main() { scanf(" %d %d %d", &EnemyNum, &DirectDamage, &BlastDamage); for (int I = 0; I < EnemyNum; I++) { scanf(" %d", &EnemyHealth[I]); EnemyHealth[I]++; } int HitNum = 0; int LeftmostRequiredHitNum = (EnemyHealth[0] + BlastDamage - 1) / BlastDamage; int RightmostRequiredHitNum = (EnemyHealth[EnemyNum - 1] + BlastDamage - 1) / BlastDamage; if (EnemyNum == 3) { LeftmostRequiredHitNum = Max(LeftmostRequiredHitNum, RightmostRequiredHitNum); RightmostRequiredHitNum = 0; EnemyHealth[0] = EnemyHealth[EnemyNum - 1] = 0; EnemyHealth[1] -= LeftmostRequiredHitNum * DirectDamage; } else { EnemyHealth[0] = 0; EnemyHealth[1] -= LeftmostRequiredHitNum * DirectDamage; EnemyHealth[2] -= LeftmostRequiredHitNum * BlastDamage; EnemyHealth[EnemyNum - 3] -= RightmostRequiredHitNum * BlastDamage; EnemyHealth[EnemyNum - 2] -= RightmostRequiredHitNum * DirectDamage; EnemyHealth[EnemyNum - 1] = 0; } int Hit[MaxHitNum]; Search(1, Hit, 0); printf("%d\n", LeftmostRequiredHitNum + RightmostRequiredHitNum + MinHitNum); for (int I = 0; I < LeftmostRequiredHitNum; I++) { printf("%d ", 2); } for (int I = 0; I < MinHitNum; I++) { printf("%d ", MinHit[I] + 1); } for (int I = 0; I < RightmostRequiredHitNum; I++) { printf("%d ", EnemyNum - 1); } }
### Prompt Generate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> int Max(int X, int Y) { return X > Y ? X : Y; } int Min(int X, int Y) { return X < Y ? X : Y; } int EnemyNum; int DirectDamage, BlastDamage; int const MaxEnemyCount = 12; int EnemyHealth[MaxEnemyCount]; int const MaxHitNum = 100000; int MinHitNum = MaxHitNum; int MinHit[MaxHitNum]; void Search(int EnemyIndex, int Hit[], int HitNum) { if (EnemyIndex > EnemyNum - 2) { if (HitNum < MinHitNum) { MinHitNum = HitNum; memcpy(MinHit, Hit, sizeof(int) * HitNum); } } else if (EnemyIndex == EnemyNum - 2) { int DirectHitNum = (Max(EnemyHealth[EnemyIndex], 0) + DirectDamage - 1) / DirectDamage; for (int I = 0; I < DirectHitNum; I++) { Hit[HitNum + I] = EnemyIndex; } Search(EnemyIndex + 1, Hit, HitNum + DirectHitNum); } else { int PossibleHitNum = (Max(EnemyHealth[EnemyIndex], 0) + DirectDamage - 1) / DirectDamage; for (int DirectHitNum = 0; DirectHitNum <= PossibleHitNum; DirectHitNum++) { int BlastHitNum = (Max(EnemyHealth[EnemyIndex] - DirectHitNum * DirectDamage, 0) + BlastDamage - 1) / BlastDamage; EnemyHealth[EnemyIndex + 1] -= DirectHitNum * BlastDamage + BlastHitNum * DirectDamage; EnemyHealth[EnemyIndex + 2] -= BlastHitNum * BlastDamage; for (int I = 0; I < DirectHitNum; I++) { Hit[HitNum + I] = EnemyIndex; } for (int I = 0; I < BlastHitNum; I++) { Hit[HitNum + DirectHitNum + I] = EnemyIndex + 1; } Search(EnemyIndex + 1, Hit, HitNum + DirectHitNum + BlastHitNum); EnemyHealth[EnemyIndex + 1] += DirectHitNum * BlastDamage + BlastHitNum * DirectDamage; EnemyHealth[EnemyIndex + 2] += BlastHitNum * BlastDamage; } } } int main() { scanf(" %d %d %d", &EnemyNum, &DirectDamage, &BlastDamage); for (int I = 0; I < EnemyNum; I++) { scanf(" %d", &EnemyHealth[I]); EnemyHealth[I]++; } int HitNum = 0; int LeftmostRequiredHitNum = (EnemyHealth[0] + BlastDamage - 1) / BlastDamage; int RightmostRequiredHitNum = (EnemyHealth[EnemyNum - 1] + BlastDamage - 1) / BlastDamage; if (EnemyNum == 3) { LeftmostRequiredHitNum = Max(LeftmostRequiredHitNum, RightmostRequiredHitNum); RightmostRequiredHitNum = 0; EnemyHealth[0] = EnemyHealth[EnemyNum - 1] = 0; EnemyHealth[1] -= LeftmostRequiredHitNum * DirectDamage; } else { EnemyHealth[0] = 0; EnemyHealth[1] -= LeftmostRequiredHitNum * DirectDamage; EnemyHealth[2] -= LeftmostRequiredHitNum * BlastDamage; EnemyHealth[EnemyNum - 3] -= RightmostRequiredHitNum * BlastDamage; EnemyHealth[EnemyNum - 2] -= RightmostRequiredHitNum * DirectDamage; EnemyHealth[EnemyNum - 1] = 0; } int Hit[MaxHitNum]; Search(1, Hit, 0); printf("%d\n", LeftmostRequiredHitNum + RightmostRequiredHitNum + MinHitNum); for (int I = 0; I < LeftmostRequiredHitNum; I++) { printf("%d ", 2); } for (int I = 0; I < MinHitNum; I++) { printf("%d ", MinHit[I] + 1); } for (int I = 0; I < RightmostRequiredHitNum; I++) { printf("%d ", EnemyNum - 1); } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 15, maxval = 20; int dp[maxn][maxval][maxval], par[maxn][maxval][maxval], h[maxn], n, a, b; int DP(int pos, int pre = h[0], int cur = h[1]) { pre = max(-1, pre); cur = max(-1, cur); if (~dp[pos][pre + 1][cur + 1]) return dp[pos][pre + 1][cur + 1]; dp[pos][pre + 1][cur + 1] = 1000 * 1000 * 1000; int mincnt; if (pos == n - 2) { mincnt = max(max((pre + b) / b, (cur + a) / a), (h[pos + 1] + b) / b); par[pos][pre + 1][cur + 1] = mincnt; return dp[pos][pre + 1][cur + 1] = mincnt; } mincnt = (pre + b) / b; int maxcnt = max(mincnt, max((cur + a) / a, (h[pos + 1] + b) / b)); for (int i = mincnt; i <= maxcnt; i++) if (DP(pos + 1, cur - a * i, h[pos + 1] - b * i) + i < dp[pos][pre + 1][cur + 1]) { dp[pos][pre + 1][cur + 1] = DP(pos + 1, cur - a * i, h[pos + 1] - b * i) + i; par[pos][pre + 1][cur + 1] = i; } return DP(pos, pre, cur); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); memset(dp, -1, sizeof dp); cin >> n >> a >> b; for (int i = 0; i < n; i++) cin >> h[i]; cout << DP(1) << '\n'; int row = 1, pre = h[0], cur = h[1]; while (row < n - 1) { int cnt = par[row][pre + 1][cur + 1]; for (int i = 0; i < cnt; i++) cout << row + 1 << ' '; row++; pre = max(-1, cur - a * cnt); cur = max(-1, h[row] - b * cnt); } cout << '\n'; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 15, maxval = 20; int dp[maxn][maxval][maxval], par[maxn][maxval][maxval], h[maxn], n, a, b; int DP(int pos, int pre = h[0], int cur = h[1]) { pre = max(-1, pre); cur = max(-1, cur); if (~dp[pos][pre + 1][cur + 1]) return dp[pos][pre + 1][cur + 1]; dp[pos][pre + 1][cur + 1] = 1000 * 1000 * 1000; int mincnt; if (pos == n - 2) { mincnt = max(max((pre + b) / b, (cur + a) / a), (h[pos + 1] + b) / b); par[pos][pre + 1][cur + 1] = mincnt; return dp[pos][pre + 1][cur + 1] = mincnt; } mincnt = (pre + b) / b; int maxcnt = max(mincnt, max((cur + a) / a, (h[pos + 1] + b) / b)); for (int i = mincnt; i <= maxcnt; i++) if (DP(pos + 1, cur - a * i, h[pos + 1] - b * i) + i < dp[pos][pre + 1][cur + 1]) { dp[pos][pre + 1][cur + 1] = DP(pos + 1, cur - a * i, h[pos + 1] - b * i) + i; par[pos][pre + 1][cur + 1] = i; } return DP(pos, pre, cur); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); memset(dp, -1, sizeof dp); cin >> n >> a >> b; for (int i = 0; i < n; i++) cin >> h[i]; cout << DP(1) << '\n'; int row = 1, pre = h[0], cur = h[1]; while (row < n - 1) { int cnt = par[row][pre + 1][cur + 1]; for (int i = 0; i < cnt; i++) cout << row + 1 << ' '; row++; pre = max(-1, cur - a * cnt); cur = max(-1, h[row] - b * cnt); } cout << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a, b, h[15], side = 0, dsh[15], cent = 52; vector<int> sshot, cshot; void recur(int now, int tot) { if (now == n - 1) { int ok = 1; for (int i = 1; i < n - 1; i++) if (dsh[i] * a + dsh[i - 1] * b + dsh[i + 1] * b <= h[i]) { ok = 0; break; } if (ok == 1 && tot < cent) { cent = tot; cshot.clear(); for (int i = 1; i < n - 1; i++) for (int j = 0; j < dsh[i]; j++) cshot.push_back(i); } return; } if (tot >= cent) return; for (int i = 0; i <= 8; i++) { dsh[now] = i; recur(now + 1, tot + i); } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); } while (h[0] >= 0) { h[0] -= b; h[2] -= b; h[1] -= a; side++; sshot.push_back(1); } while (h[n - 1] >= 0) { h[n - 1] -= b; h[n - 3] -= b; h[n - 2] -= a; side++; sshot.push_back(n - 2); } memset(dsh, 0, sizeof(dsh)); recur(1, 0); printf("%d\n", cent + side); printf("%d", sshot[0] + 1); for (int i = 1; i < sshot.size(); i++) printf(" %d", sshot[i] + 1); for (int i = 0; i < cshot.size(); i++) printf(" %d", cshot[i] + 1); printf("\n"); }
### Prompt Create a solution in Cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b, h[15], side = 0, dsh[15], cent = 52; vector<int> sshot, cshot; void recur(int now, int tot) { if (now == n - 1) { int ok = 1; for (int i = 1; i < n - 1; i++) if (dsh[i] * a + dsh[i - 1] * b + dsh[i + 1] * b <= h[i]) { ok = 0; break; } if (ok == 1 && tot < cent) { cent = tot; cshot.clear(); for (int i = 1; i < n - 1; i++) for (int j = 0; j < dsh[i]; j++) cshot.push_back(i); } return; } if (tot >= cent) return; for (int i = 0; i <= 8; i++) { dsh[now] = i; recur(now + 1, tot + i); } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); } while (h[0] >= 0) { h[0] -= b; h[2] -= b; h[1] -= a; side++; sshot.push_back(1); } while (h[n - 1] >= 0) { h[n - 1] -= b; h[n - 3] -= b; h[n - 2] -= a; side++; sshot.push_back(n - 2); } memset(dsh, 0, sizeof(dsh)); recur(1, 0); printf("%d\n", cent + side); printf("%d", sshot[0] + 1); for (int i = 1; i < sshot.size(); i++) printf(" %d", sshot[i] + 1); for (int i = 0; i < cshot.size(); i++) printf(" %d", cshot[i] + 1); printf("\n"); } ```
#include <bits/stdc++.h> using namespace std; int n, i, j, a, b, r = 1000000000, h[15], z[15], c[15]; void rec(int l, int s) { if (s >= r) return; int aa = h[l - 1], bb = h[l], cc = h[l + 1]; if (l == n - 2) { while (h[l - 1] >= 0 || h[l] >= 0 || h[l + 1] >= 0) { h[l - 1] -= b; h[l] -= a; h[l + 1] -= b; z[l]++; s++; } if (s < r) { r = s; for (int i = 0; i < n; i++) c[i] = z[i]; } h[l - 1] = aa; h[l] = bb; h[l + 1] = cc; z[l] = 0; return; } if (h[l - 1] < 0) rec(l + 1, s); while (h[l - 1] >= 0 || h[l] >= 0 || h[l + 1] >= 0) { h[l - 1] -= b; h[l] -= a; h[l + 1] -= b; z[l]++; if (h[l - 1] < 0) rec(l + 1, s + z[l]); } h[l - 1] = aa; h[l] = bb; h[l + 1] = cc; z[l] = 0; } int main() { scanf("%d%d%d", &n, &a, &b); for (i = 0; i < n; i++) scanf("%d", &h[i]); rec(1, 0); printf("%d\n", r); for (i = 1; i < n - 2; i++) for (j = 0; j < c[i]; j++) printf("%d ", i + 1); for (j = 1; j < c[n - 2]; j++) printf("%d ", n - 1); printf("%d\n", n - 1); return 0; }
### Prompt Please create a solution in Cpp to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, i, j, a, b, r = 1000000000, h[15], z[15], c[15]; void rec(int l, int s) { if (s >= r) return; int aa = h[l - 1], bb = h[l], cc = h[l + 1]; if (l == n - 2) { while (h[l - 1] >= 0 || h[l] >= 0 || h[l + 1] >= 0) { h[l - 1] -= b; h[l] -= a; h[l + 1] -= b; z[l]++; s++; } if (s < r) { r = s; for (int i = 0; i < n; i++) c[i] = z[i]; } h[l - 1] = aa; h[l] = bb; h[l + 1] = cc; z[l] = 0; return; } if (h[l - 1] < 0) rec(l + 1, s); while (h[l - 1] >= 0 || h[l] >= 0 || h[l + 1] >= 0) { h[l - 1] -= b; h[l] -= a; h[l + 1] -= b; z[l]++; if (h[l - 1] < 0) rec(l + 1, s + z[l]); } h[l - 1] = aa; h[l] = bb; h[l + 1] = cc; z[l] = 0; } int main() { scanf("%d%d%d", &n, &a, &b); for (i = 0; i < n; i++) scanf("%d", &h[i]); rec(1, 0); printf("%d\n", r); for (i = 1; i < n - 2; i++) for (j = 0; j < c[i]; j++) printf("%d ", i + 1); for (j = 1; j < c[n - 2]; j++) printf("%d ", n - 1); printf("%d\n", n - 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int dp[20][20][20], n, a, b, h[20], track[20][20][20][5]; void go(int ind, int health, int hits) { if (ind > n) return; for (int i = 1; i <= hits; ++i) cout << ind << " "; go(ind + 1, track[ind][health][hits][1], track[ind][health][hits][2]); } int main() { cin >> n >> a >> b; for (int i = int(1); i <= (int)n; i++) { cin >> h[i]; h[i]++; } for (int w = 1; w <= n; ++w) for (int i = 0; i <= 16; ++i) for (int j = 0; j <= 16; ++j) dp[w][i][j] = 1e9; dp[n][h[n]][0] = 0; for (int i = n - 1; i >= 2; i--) { for (int health = 0; health <= 16; ++health) { for (int hitsp = 0; hitsp <= 16; ++hitsp) { for (int hitsnow = 0; hitsnow <= 16; ++hitsnow) { if (hitsnow * b >= health) { if (dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow] > hitsnow + dp[i + 1][health][hitsp]) { dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow] = min(dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow], hitsnow + dp[i + 1][health][hitsp]); track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][0] = i + 1; track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][1] = health; track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][2] = hitsp; } } } } } } int hhit, hhealth = 0; int ans = 2e9; for (int hits = 0; hits <= 16; ++hits) { if (hits * b >= h[1]) { if (ans > dp[2][0][hits]) { ans = min(ans, dp[2][0][hits]); hhit = hits; } } } cout << ans << endl; go(2, hhealth, hhit); return 0; }
### Prompt In Cpp, your task is to solve the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int dp[20][20][20], n, a, b, h[20], track[20][20][20][5]; void go(int ind, int health, int hits) { if (ind > n) return; for (int i = 1; i <= hits; ++i) cout << ind << " "; go(ind + 1, track[ind][health][hits][1], track[ind][health][hits][2]); } int main() { cin >> n >> a >> b; for (int i = int(1); i <= (int)n; i++) { cin >> h[i]; h[i]++; } for (int w = 1; w <= n; ++w) for (int i = 0; i <= 16; ++i) for (int j = 0; j <= 16; ++j) dp[w][i][j] = 1e9; dp[n][h[n]][0] = 0; for (int i = n - 1; i >= 2; i--) { for (int health = 0; health <= 16; ++health) { for (int hitsp = 0; hitsp <= 16; ++hitsp) { for (int hitsnow = 0; hitsnow <= 16; ++hitsnow) { if (hitsnow * b >= health) { if (dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow] > hitsnow + dp[i + 1][health][hitsp]) { dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow] = min(dp[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow], hitsnow + dp[i + 1][health][hitsp]); track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][0] = i + 1; track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][1] = health; track[i][max(0, h[i] - hitsnow * a - b * hitsp)][hitsnow][2] = hitsp; } } } } } } int hhit, hhealth = 0; int ans = 2e9; for (int hits = 0; hits <= 16; ++hits) { if (hits * b >= h[1]) { if (ans > dp[2][0][hits]) { ans = min(ans, dp[2][0][hits]); hhit = hits; } } } cout << ans << endl; go(2, hhealth, hhit); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, a, b; long long h[20]; long long dp[20][20][20]; long long num[20][20][20]; long long hl[20][20][20]; vector<long long> v; long long rec(long long i, long long j, long long k) { if (i == n - 1) { if (j > 0) return 2000000000000000005; else return 0; } if (dp[i][j][k] < 5000) { return dp[i][j][k]; } long long newh = h[i] - k * b; for (long long first = 0; first < 16; first++) { if (first * b < j) continue; long long second = first + rec(i + 1, max(0LL, newh - first * a), first); if (second < dp[i][j][k]) { dp[i][j][k] = second; num[i][j][k] = first; hl[i][j][k] = max(0LL, newh - first * a); } } return dp[i][j][k]; } void finalize(long long i, long long j, long long k) { if (i == n - 1) { return; } long long first = num[i][j][k]; while (first--) v.push_back(i); finalize(i + 1, hl[i][j][k], num[i][j][k]); } void solve() { cin >> n >> a >> b; for (long long i = 0; i < n; i++) { cin >> h[i]; h[i]++; } memset(dp, 2000000000000000005, sizeof dp); while (h[0] > 0) { h[1] -= a; h[0] -= b; h[2] -= b; h[1] = max(h[1], 0LL); h[2] = max(h[2], 0LL); h[0] = max(h[0], 0LL); v.push_back(1); } while (h[n - 1] > 0) { h[n - 1] -= b; h[n - 2] -= a; h[n - 3] -= b; h[n - 1] = max(h[n - 1], 0LL); h[n - 2] = max(h[n - 2], 0LL); h[n - 3] = max(h[n - 3], 0LL); v.push_back(n - 2); } rec(1, 0, 0); finalize(1, 0, 0); cout << v.size() << "\n"; for (auto it : v) { cout << it + 1 << " "; } cout << "\n"; } signed main() { ios_base ::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
### Prompt Please formulate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, a, b; long long h[20]; long long dp[20][20][20]; long long num[20][20][20]; long long hl[20][20][20]; vector<long long> v; long long rec(long long i, long long j, long long k) { if (i == n - 1) { if (j > 0) return 2000000000000000005; else return 0; } if (dp[i][j][k] < 5000) { return dp[i][j][k]; } long long newh = h[i] - k * b; for (long long first = 0; first < 16; first++) { if (first * b < j) continue; long long second = first + rec(i + 1, max(0LL, newh - first * a), first); if (second < dp[i][j][k]) { dp[i][j][k] = second; num[i][j][k] = first; hl[i][j][k] = max(0LL, newh - first * a); } } return dp[i][j][k]; } void finalize(long long i, long long j, long long k) { if (i == n - 1) { return; } long long first = num[i][j][k]; while (first--) v.push_back(i); finalize(i + 1, hl[i][j][k], num[i][j][k]); } void solve() { cin >> n >> a >> b; for (long long i = 0; i < n; i++) { cin >> h[i]; h[i]++; } memset(dp, 2000000000000000005, sizeof dp); while (h[0] > 0) { h[1] -= a; h[0] -= b; h[2] -= b; h[1] = max(h[1], 0LL); h[2] = max(h[2], 0LL); h[0] = max(h[0], 0LL); v.push_back(1); } while (h[n - 1] > 0) { h[n - 1] -= b; h[n - 2] -= a; h[n - 3] -= b; h[n - 1] = max(h[n - 1], 0LL); h[n - 2] = max(h[n - 2], 0LL); h[n - 3] = max(h[n - 3], 0LL); v.push_back(n - 2); } rec(1, 0, 0); finalize(1, 0, 0); cout << v.size() << "\n"; for (auto it : v) { cout << it + 1 << " "; } cout << "\n"; } signed main() { ios_base ::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; } ```
#include <bits/stdc++.h> namespace FastIO { const int SIZ = 1 << 21 | 1; char *iS, *iT, ibuff[SIZ], obuff[SIZ], *oS = obuff, *oT = oS + SIZ - 1, fu[110], cc; int fr; inline void out() { fwrite(obuff, 1, oS - obuff, stdout); oS = obuff; } template <class Type> inline void read(Type& x) { x = 0; Type y = 1; for (cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++); (cc > '9' || cc < '0') && cc != '-'; cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; cc == '-' ? y = -1 : x = (cc & 15); for (cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++); cc >= '0' && cc <= '9'; cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (cc & 15); x *= y; } template <class Type> inline void print(Type x, char text = '\n') { if (x < 0) *oS++ = '-', x *= -1; if (x == 0) *oS++ = '0'; while (x) fu[++fr] = x % 10 + '0', x /= 10; while (fr) ; *oS++ = fu[fr--]; *oS++ = text; out(); } inline void prints(char x[], char text = '\n') { for (register int i = 0; x[i]; ++i) *oS++ = x[i]; *oS++ = text; out(); } } // namespace FastIO using namespace FastIO; template <typename T> inline T max(T a, T b) { return (a > b) ? a : b; } template <typename T> inline T min(T a, T b) { return (a > b) ? b : a; } const int MAXN = 10 + 10; int dp[MAXN][MAXN][MAXN], minx[MAXN][MAXN][MAXN], pro[MAXN]; int n, a, b, hp[MAXN], posi = 1, posj, posk; int main(int argc, char* argv[]) { read(n); read(a); read(b); for (int i = (1); i <= (n); i += (1)) read(hp[i]); memset(dp, (1 << 6) - 1, sizeof dp); for (int i = (0); i <= (16); i += (1)) if (b * i > hp[n]) dp[n][i][0] = 0; for (int i = (0); i <= (16); i += (1)) pro[i] = i / b + 1; for (int i = (n); i >= (1); i -= (1)) { if (i < n) for (int j = (0); j <= (16); j += (1)) { int v = hp[i] - j * b; for (int k = (0); k <= (16); k += (1)) dp[i][j][k] = k + minx[i + 1][k][v < 0 ? 0 : pro[v]], v -= a; } for (int j = (0); j <= (16); j += (1)) { minx[i][j][16] = dp[i][j][16]; for (int k = (15); k >= (0); k -= (1)) minx[i][j][k] = min(minx[i][j][k + 1], dp[i][j][k]); } } printf("%d\n", dp[1][0][0]); while (++posi < n) { int v = hp[posi - 1] - posj * b - posk * a; int l = (v < 0) ? 0 : pro[v]; while (dp[posi][posk][l] != dp[posi - 1][posj][posk] - posk) ++l; posj = posk; posk = l; for (int i = (1); i <= (l); i += (1)) printf("%d ", posi); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> namespace FastIO { const int SIZ = 1 << 21 | 1; char *iS, *iT, ibuff[SIZ], obuff[SIZ], *oS = obuff, *oT = oS + SIZ - 1, fu[110], cc; int fr; inline void out() { fwrite(obuff, 1, oS - obuff, stdout); oS = obuff; } template <class Type> inline void read(Type& x) { x = 0; Type y = 1; for (cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++); (cc > '9' || cc < '0') && cc != '-'; cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; cc == '-' ? y = -1 : x = (cc & 15); for (cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++); cc >= '0' && cc <= '9'; cc = (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (cc & 15); x *= y; } template <class Type> inline void print(Type x, char text = '\n') { if (x < 0) *oS++ = '-', x *= -1; if (x == 0) *oS++ = '0'; while (x) fu[++fr] = x % 10 + '0', x /= 10; while (fr) ; *oS++ = fu[fr--]; *oS++ = text; out(); } inline void prints(char x[], char text = '\n') { for (register int i = 0; x[i]; ++i) *oS++ = x[i]; *oS++ = text; out(); } } // namespace FastIO using namespace FastIO; template <typename T> inline T max(T a, T b) { return (a > b) ? a : b; } template <typename T> inline T min(T a, T b) { return (a > b) ? b : a; } const int MAXN = 10 + 10; int dp[MAXN][MAXN][MAXN], minx[MAXN][MAXN][MAXN], pro[MAXN]; int n, a, b, hp[MAXN], posi = 1, posj, posk; int main(int argc, char* argv[]) { read(n); read(a); read(b); for (int i = (1); i <= (n); i += (1)) read(hp[i]); memset(dp, (1 << 6) - 1, sizeof dp); for (int i = (0); i <= (16); i += (1)) if (b * i > hp[n]) dp[n][i][0] = 0; for (int i = (0); i <= (16); i += (1)) pro[i] = i / b + 1; for (int i = (n); i >= (1); i -= (1)) { if (i < n) for (int j = (0); j <= (16); j += (1)) { int v = hp[i] - j * b; for (int k = (0); k <= (16); k += (1)) dp[i][j][k] = k + minx[i + 1][k][v < 0 ? 0 : pro[v]], v -= a; } for (int j = (0); j <= (16); j += (1)) { minx[i][j][16] = dp[i][j][16]; for (int k = (15); k >= (0); k -= (1)) minx[i][j][k] = min(minx[i][j][k + 1], dp[i][j][k]); } } printf("%d\n", dp[1][0][0]); while (++posi < n) { int v = hp[posi - 1] - posj * b - posk * a; int l = (v < 0) ? 0 : pro[v]; while (dp[posi][posk][l] != dp[posi - 1][posj][posk] - posk) ++l; posj = posk; posk = l; for (int i = (1); i <= (l); i += (1)) printf("%d ", posi); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; inline int get_state(ll s, int x) { return ((s >> (5 * x)) & 0x1f); } inline ll set_state(ll s, ll v, int x) { return ((s & ~(0x1fLL << (5 * x))) | (v << (5 * x))); } int cs[10]; map<ll, int> st; map<ll, ii> ps; bool kill_all(ll s, int n, int a, int b) { for (int i = 2; i <= n - 1; ++i) if (cs[i] > b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) return false; return true; } bool all_dead(ll s, int pos, int a, int b) { for (int i = pos - 1; i <= pos + 1; ++i) { if (cs[i] > b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) return false; } return true; } int dp(ll s, int n, int a, int b) { if (s == 0) return 0; auto it = st.find(s); if (it != st.end()) { return it->second; } auto res = 1000000000; for (int i = 2; i <= n - 1; ++i) { if (get_state(s, i - 1) or get_state(s, i) or get_state(s, i + 1)) { auto r = set_state(s, max(get_state(s, i) - a, 0), i); r = set_state(r, max(get_state(r, i - 1) - b, 0), i - 1); r = set_state(r, max(get_state(r, i + 1) - b, 0), i + 1); auto value = dp(r, n, a, b) + 1; if (value < res) { res = value; ps[s] = ii(r, i); } } } st[s] = res; return res; } vector<int> solve(int n, int a, int b) { vector<int> shoots; ll s = 0; for (int i = 1; i <= n; ++i) s = set_state(s, cs[i], i); while (cs[1] > 0) { shoots.push_back(2); cs[1] -= b; cs[2] -= a; cs[3] -= b; s = set_state(s, max(get_state(s, 1) - b, 0), 1); s = set_state(s, max(get_state(s, 2) - a, 0), 2); s = set_state(s, max(get_state(s, 3) - b, 0), 3); } while (cs[n] > 0) { shoots.push_back(n - 1); cs[n] -= b; cs[n - 1] -= a; cs[n - 2] -= b; s = set_state(s, max(get_state(s, n - 2) - b, 0), n - 2); s = set_state(s, max(get_state(s, n - 1) - a, 0), n - 1); s = set_state(s, max(get_state(s, n) - b, 0), n); } dp(s, n, a, b); auto it = ps.find(s); while (it != ps.end()) { s = it->second.first; shoots.push_back(it->second.second); it = ps.find(s); } for (int i = 2; i <= n - 1; ++i) for (int j = 0; j < get_state(s, i); ++j) shoots.push_back(i); return shoots; } int main() { int n, a, b; cin >> n >> a >> b; for (int i = 1; i <= n; ++i) { cin >> cs[i]; ++cs[i]; } auto shoots = solve(n, a, b); cout << shoots.size() << endl; for (size_t i = 0; i < shoots.size(); ++i) printf("%d%c", shoots[i], " \n"[i + 1 == shoots.size()]); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; inline int get_state(ll s, int x) { return ((s >> (5 * x)) & 0x1f); } inline ll set_state(ll s, ll v, int x) { return ((s & ~(0x1fLL << (5 * x))) | (v << (5 * x))); } int cs[10]; map<ll, int> st; map<ll, ii> ps; bool kill_all(ll s, int n, int a, int b) { for (int i = 2; i <= n - 1; ++i) if (cs[i] > b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) return false; return true; } bool all_dead(ll s, int pos, int a, int b) { for (int i = pos - 1; i <= pos + 1; ++i) { if (cs[i] > b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) return false; } return true; } int dp(ll s, int n, int a, int b) { if (s == 0) return 0; auto it = st.find(s); if (it != st.end()) { return it->second; } auto res = 1000000000; for (int i = 2; i <= n - 1; ++i) { if (get_state(s, i - 1) or get_state(s, i) or get_state(s, i + 1)) { auto r = set_state(s, max(get_state(s, i) - a, 0), i); r = set_state(r, max(get_state(r, i - 1) - b, 0), i - 1); r = set_state(r, max(get_state(r, i + 1) - b, 0), i + 1); auto value = dp(r, n, a, b) + 1; if (value < res) { res = value; ps[s] = ii(r, i); } } } st[s] = res; return res; } vector<int> solve(int n, int a, int b) { vector<int> shoots; ll s = 0; for (int i = 1; i <= n; ++i) s = set_state(s, cs[i], i); while (cs[1] > 0) { shoots.push_back(2); cs[1] -= b; cs[2] -= a; cs[3] -= b; s = set_state(s, max(get_state(s, 1) - b, 0), 1); s = set_state(s, max(get_state(s, 2) - a, 0), 2); s = set_state(s, max(get_state(s, 3) - b, 0), 3); } while (cs[n] > 0) { shoots.push_back(n - 1); cs[n] -= b; cs[n - 1] -= a; cs[n - 2] -= b; s = set_state(s, max(get_state(s, n - 2) - b, 0), n - 2); s = set_state(s, max(get_state(s, n - 1) - a, 0), n - 1); s = set_state(s, max(get_state(s, n) - b, 0), n); } dp(s, n, a, b); auto it = ps.find(s); while (it != ps.end()) { s = it->second.first; shoots.push_back(it->second.second); it = ps.find(s); } for (int i = 2; i <= n - 1; ++i) for (int j = 0; j < get_state(s, i); ++j) shoots.push_back(i); return shoots; } int main() { int n, a, b; cin >> n >> a >> b; for (int i = 1; i <= n; ++i) { cin >> cs[i]; ++cs[i]; } auto shoots = solve(n, a, b); cout << shoots.size() << endl; for (size_t i = 0; i < shoots.size(); ++i) printf("%d%c", shoots[i], " \n"[i + 1 == shoots.size()]); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; b /= 2; a = ((a % m) * (a % m)) % m; } return ans % m; } long long h[200005]; int n, a, b; long long dp[12][20][20][20]; long long solve(int id, int prev, int curr, int next) { if (prev < 0) prev = -1; if (curr < 0) curr = -1; if (next < 0) next = -1; long long ans = (long long)5e15; if (dp[id][prev + 1][curr + 1][next + 1] != -1) return dp[id][prev + 1][curr + 1][next + 1]; if (id == n - 2) { if (prev < 0 && curr < 0 && next < 0) return 0; ans = min(ans, 1 + solve(id, prev - b, curr - a, next - b)); } else { if (prev >= 0 || curr >= 0 || next >= 0) { ans = min(ans, 1 + solve(id, prev - b, curr - a, next - b)); } if (prev < 0) { ans = min(ans, solve(id + 1, curr, next, h[id + 2])); } } return dp[id][prev + 1][curr + 1][next + 1] = ans; } void printpath(int id, int prev, int curr, int next) { if (prev < 0) prev = -1; if (curr < 0) curr = -1; if (next < 0) next = -1; if (id == n - 2) { if (prev < 0 && curr < 0 && next < 0) return; cout << id + 1 << " "; printpath(id, prev - b, curr - a, next - b); return; } else { if (1 + solve(id, prev - b, curr - a, next - b) == dp[id][prev + 1][curr + 1][next + 1]) { cout << id + 1 << " "; printpath(id, prev - b, curr - a, next - b); } else { printpath(id + 1, curr, next, h[id + 2]); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> a >> b; for (int i = 0; i < n; i++) { cin >> h[i]; } memset(dp, -1, sizeof dp); cout << solve(1, h[0], h[1], h[2]) << "\n"; printpath(1, h[0], h[1], h[2]); }
### Prompt Create a solution in cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; b /= 2; a = ((a % m) * (a % m)) % m; } return ans % m; } long long h[200005]; int n, a, b; long long dp[12][20][20][20]; long long solve(int id, int prev, int curr, int next) { if (prev < 0) prev = -1; if (curr < 0) curr = -1; if (next < 0) next = -1; long long ans = (long long)5e15; if (dp[id][prev + 1][curr + 1][next + 1] != -1) return dp[id][prev + 1][curr + 1][next + 1]; if (id == n - 2) { if (prev < 0 && curr < 0 && next < 0) return 0; ans = min(ans, 1 + solve(id, prev - b, curr - a, next - b)); } else { if (prev >= 0 || curr >= 0 || next >= 0) { ans = min(ans, 1 + solve(id, prev - b, curr - a, next - b)); } if (prev < 0) { ans = min(ans, solve(id + 1, curr, next, h[id + 2])); } } return dp[id][prev + 1][curr + 1][next + 1] = ans; } void printpath(int id, int prev, int curr, int next) { if (prev < 0) prev = -1; if (curr < 0) curr = -1; if (next < 0) next = -1; if (id == n - 2) { if (prev < 0 && curr < 0 && next < 0) return; cout << id + 1 << " "; printpath(id, prev - b, curr - a, next - b); return; } else { if (1 + solve(id, prev - b, curr - a, next - b) == dp[id][prev + 1][curr + 1][next + 1]) { cout << id + 1 << " "; printpath(id, prev - b, curr - a, next - b); } else { printpath(id + 1, curr, next, h[id + 2]); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> a >> b; for (int i = 0; i < n; i++) { cin >> h[i]; } memset(dp, -1, sizeof dp); cout << solve(1, h[0], h[1], h[2]) << "\n"; printpath(1, h[0], h[1], h[2]); } ```
#include <bits/stdc++.h> using namespace std; int n, a, b; int h[100]; int cnt = 1e9; vector<int> o, temp; void solve(int pos, int cost) { if (cost >= cnt) return; if (pos == n - 1) { if (h[n - 1] < 0) { cnt = cost; o = temp; } return; } for (int i = 0; i <= max(h[pos - 1] / b, max(h[pos] / a, h[pos + 1] / b)) + 1; i++) { if (h[pos - 1] - b * i >= 0) continue; h[pos - 1] -= b * i; h[pos] -= a * i; h[pos + 1] -= b * i; for (int j = 0; j < i; j++) temp.push_back(pos); solve(pos + 1, cost + i); h[pos - 1] += b * i; h[pos] += a * i; h[pos + 1] += b * i; for (int j = 0; j < i; j++) temp.pop_back(); } } int main() { cin >> n >> a >> b; for (int i = 0; i < n; i++) { cin >> h[i]; } solve(1, 0); cout << cnt << endl; for (auto i : o) cout << i + 1 << " "; cout << endl; }
### Prompt In CPP, your task is to solve the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b; int h[100]; int cnt = 1e9; vector<int> o, temp; void solve(int pos, int cost) { if (cost >= cnt) return; if (pos == n - 1) { if (h[n - 1] < 0) { cnt = cost; o = temp; } return; } for (int i = 0; i <= max(h[pos - 1] / b, max(h[pos] / a, h[pos + 1] / b)) + 1; i++) { if (h[pos - 1] - b * i >= 0) continue; h[pos - 1] -= b * i; h[pos] -= a * i; h[pos + 1] -= b * i; for (int j = 0; j < i; j++) temp.push_back(pos); solve(pos + 1, cost + i); h[pos - 1] += b * i; h[pos] += a * i; h[pos + 1] += b * i; for (int j = 0; j < i; j++) temp.pop_back(); } } int main() { cin >> n >> a >> b; for (int i = 0; i < n; i++) { cin >> h[i]; } solve(1, 0); cout << cnt << endl; for (auto i : o) cout << i + 1 << " "; cout << endl; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &k) { k = 0; int flag = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') flag = -1; c = getchar(); } while (c >= '0' && c <= '9') { k = k * 10 + c - '0'; c = getchar(); } k *= flag; } const int N = 11; int n, m, k, s, t, k1, k2, p, a, b; int f[N][16][16], h[N], g[N][16][16], v[N][16][16]; int main() { read(n); read(a); read(b); for (int i = 1; i <= n; ++i) { read(h[i]); h[i]++; } if (h[1] % b == 0) k1 = h[1] / b; else k1 = h[1] / b + 1; h[2] -= k1 * a; h[3] -= k1 * b; for (int i = 1; i <= n; ++i) h[i] = max(h[i], 0); if (h[n] % b == 0) k2 = h[n] / b; else k2 = h[n] / b + 1; h[n - 1] -= k2 * a; h[n - 2] -= k2 * b; for (int i = 1; i <= n; ++i) h[i] = max(h[i], 0); h[1] = h[n] = 0; memset(f, 0x3f, sizeof(f)); f[1][0][0] = 0; for (int i = 1; i <= n - 2; ++i) for (int j = 0; j <= 15; ++j) for (int k = 0; k <= 15; ++k) if (f[i][j][k] < 0x3f3f3f3f) for (int l = 0; l <= 15; ++l) if ((j + l) * b + k * a >= h[i] && f[i + 1][k][l] > f[i][j][k] + l) { f[i + 1][k][l] = f[i][j][k] + l; g[i + 1][k][l] = j; } int ans = 0x3f3f3f3f; for (int i = 0; i <= 15; ++i) for (int j = 0; j <= 15; ++j) if (i * b + j * a >= h[n - 1] && ans > f[n - 1][i][j]) { ans = f[n - 1][i][j]; s = i; t = j; } cout << ans + k1 + k2 << endl; for (int i = 1; i <= k1; ++i) cout << 2 << ' '; for (int i = 1; i <= k2; ++i) cout << n - 1 << ' '; k = n - 1; while (k != 1) { for (int i = 1; i <= t; ++i) cout << k << ' '; p = g[k][s][t]; t = s; s = p; --k; } }
### Prompt Create a solution in Cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &k) { k = 0; int flag = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') flag = -1; c = getchar(); } while (c >= '0' && c <= '9') { k = k * 10 + c - '0'; c = getchar(); } k *= flag; } const int N = 11; int n, m, k, s, t, k1, k2, p, a, b; int f[N][16][16], h[N], g[N][16][16], v[N][16][16]; int main() { read(n); read(a); read(b); for (int i = 1; i <= n; ++i) { read(h[i]); h[i]++; } if (h[1] % b == 0) k1 = h[1] / b; else k1 = h[1] / b + 1; h[2] -= k1 * a; h[3] -= k1 * b; for (int i = 1; i <= n; ++i) h[i] = max(h[i], 0); if (h[n] % b == 0) k2 = h[n] / b; else k2 = h[n] / b + 1; h[n - 1] -= k2 * a; h[n - 2] -= k2 * b; for (int i = 1; i <= n; ++i) h[i] = max(h[i], 0); h[1] = h[n] = 0; memset(f, 0x3f, sizeof(f)); f[1][0][0] = 0; for (int i = 1; i <= n - 2; ++i) for (int j = 0; j <= 15; ++j) for (int k = 0; k <= 15; ++k) if (f[i][j][k] < 0x3f3f3f3f) for (int l = 0; l <= 15; ++l) if ((j + l) * b + k * a >= h[i] && f[i + 1][k][l] > f[i][j][k] + l) { f[i + 1][k][l] = f[i][j][k] + l; g[i + 1][k][l] = j; } int ans = 0x3f3f3f3f; for (int i = 0; i <= 15; ++i) for (int j = 0; j <= 15; ++j) if (i * b + j * a >= h[n - 1] && ans > f[n - 1][i][j]) { ans = f[n - 1][i][j]; s = i; t = j; } cout << ans + k1 + k2 << endl; for (int i = 1; i <= k1; ++i) cout << 2 << ' '; for (int i = 1; i <= k2; ++i) cout << n - 1 << ' '; k = n - 1; while (k != 1) { for (int i = 1; i <= t; ++i) cout << k << ' '; p = g[k][s][t]; t = s; s = p; --k; } } ```
#include <bits/stdc++.h> using namespace std; int n, a, b; int cv(int cz, int mv) { if (cz < 0) return 0; return cz / mv + 1; } int minnumshurt(vector<int>& zh, int i, vector<int>& nv) { if (zh.size() - 2 == i) { int res = 0; res = max(cv(zh[i - 1], b), res); res = max(cv(zh[i], a), res); res = max(cv(zh[i + 1], b), res); for (int j = 0; j < res; j++) nv.push_back(i); return res; } int res = cv(zh[i - 1], b); zh[i - 1] -= res * b; zh[i] -= res * a; zh[i + 1] -= res * b; int minres = minnumshurt(zh, i + 1, nv) + res; for (int j = 0; j < res; j++) nv.push_back(i); while (zh[i] >= 0) { zh[i - 1] -= b; zh[i] -= a; zh[i + 1] -= b; res++; vector<int> nnv; int cur = minnumshurt(zh, i + 1, nnv) + res; if (cur < minres) { swap(nnv, nv); for (int j = 0; j < res; j++) nv.push_back(i); minres = min(cur, minres); } } zh[i - 1] += res * b; zh[i] += res * a; zh[i + 1] += res * b; return minres; } int main() { cin >> n >> a >> b; vector<int> zh(n); for (auto& z : zh) cin >> z; vector<int> nnv; cout << minnumshurt(zh, 1, nnv) << endl; for (auto v : nnv) cout << v + 1 << " "; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b; int cv(int cz, int mv) { if (cz < 0) return 0; return cz / mv + 1; } int minnumshurt(vector<int>& zh, int i, vector<int>& nv) { if (zh.size() - 2 == i) { int res = 0; res = max(cv(zh[i - 1], b), res); res = max(cv(zh[i], a), res); res = max(cv(zh[i + 1], b), res); for (int j = 0; j < res; j++) nv.push_back(i); return res; } int res = cv(zh[i - 1], b); zh[i - 1] -= res * b; zh[i] -= res * a; zh[i + 1] -= res * b; int minres = minnumshurt(zh, i + 1, nv) + res; for (int j = 0; j < res; j++) nv.push_back(i); while (zh[i] >= 0) { zh[i - 1] -= b; zh[i] -= a; zh[i + 1] -= b; res++; vector<int> nnv; int cur = minnumshurt(zh, i + 1, nnv) + res; if (cur < minres) { swap(nnv, nv); for (int j = 0; j < res; j++) nv.push_back(i); minres = min(cur, minres); } } zh[i - 1] += res * b; zh[i] += res * a; zh[i + 1] += res * b; return minres; } int main() { cin >> n >> a >> b; vector<int> zh(n); for (auto& z : zh) cin >> z; vector<int> nnv; cout << minnumshurt(zh, 1, nnv) << endl; for (auto v : nnv) cout << v + 1 << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& num) { char CH; bool F = false; for (CH = getchar(); CH < '0' || CH > '9'; F = CH == '-', CH = getchar()) ; for (num = 0; CH >= '0' && CH <= '9'; num = num * 10 + CH - '0', CH = getchar()) ; F && (num = -num); } int stk[70], tp; template <class T> inline void print(T p) { if (!p) { puts("0"); return; } tp = 0; while (p) stk[++tp] = p % 10, p /= 10; while (tp) putchar(stk[tp--] + '0'); putchar('\n'); } int n, a, b, h[23], ans[100], an; bool dfs(int tms, int x, int pos) { if (tms == 0) { for (int i = 1; i <= n; ++i) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(tms, x + 1, pos); if (x == n) { h[n] -= b; ans[tms] = n - 1; if (dfs(tms - 1, x, pos)) return true; h[n] += b; } else { for (int i = max(x, pos); i <= x + 1; ++i) if (i > 1 && i < n) { ans[tms] = i; h[i] -= a; h[i - 1] -= b; h[i + 1] -= b; if (dfs(tms - 1, x, i)) return true; h[i] += a; h[i - 1] += b; h[i + 1] += b; } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) read(h[i]); for (an = 1;; ++an) if (dfs(an, 1, 2)) { print(an); for (int i = an; i; --i) printf("%d ", ans[i]); return 0; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> void read(T& num) { char CH; bool F = false; for (CH = getchar(); CH < '0' || CH > '9'; F = CH == '-', CH = getchar()) ; for (num = 0; CH >= '0' && CH <= '9'; num = num * 10 + CH - '0', CH = getchar()) ; F && (num = -num); } int stk[70], tp; template <class T> inline void print(T p) { if (!p) { puts("0"); return; } tp = 0; while (p) stk[++tp] = p % 10, p /= 10; while (tp) putchar(stk[tp--] + '0'); putchar('\n'); } int n, a, b, h[23], ans[100], an; bool dfs(int tms, int x, int pos) { if (tms == 0) { for (int i = 1; i <= n; ++i) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(tms, x + 1, pos); if (x == n) { h[n] -= b; ans[tms] = n - 1; if (dfs(tms - 1, x, pos)) return true; h[n] += b; } else { for (int i = max(x, pos); i <= x + 1; ++i) if (i > 1 && i < n) { ans[tms] = i; h[i] -= a; h[i - 1] -= b; h[i + 1] -= b; if (dfs(tms - 1, x, i)) return true; h[i] += a; h[i - 1] += b; h[i + 1] += b; } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) read(h[i]); for (an = 1;; ++an) if (dfs(an, 1, 2)) { print(an); for (int i = an; i; --i) printf("%d ", ans[i]); return 0; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 20, inf = 1e9; int h[N]; int f[N][N][N]; pair<int, int> g[N][N][N]; vector<int> ans; int main() { int n, a, b; scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]), h[i]++; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) f[i][j][k] = inf; } } f[1][0][h[1]] = 0; for (int i = 2; i < n; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) if (f[i - 1][j][k] != inf) { for (int l = 0; l < N; l++) { if (l * b >= k) { int now = h[i] - j * b - l * a; if (now < 0) now = 0; if (f[i - 1][j][k] + l < f[i][l][now]) { f[i][l][now] = f[i - 1][j][k] + l; g[i][l][now] = make_pair(j, k); } } } } } } int i = n - 1, j, k = 0, Max = inf; for (int l = 0; l < N; l++) { if (l * b >= h[n]) if (f[n - 1][l][0] < Max) { Max = f[n - 1][l][0]; j = l; } } while (i > 1) { for (int l = 0; l < j; l++) ans.push_back(i); int jj = g[i][j][k].first, kk = g[i][j][k].second; i--; j = jj; k = kk; } printf("%d\n", Max); for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]); if (ans.size()) printf("%d\n", ans[ans.size() - 1]); return 0; }
### Prompt Please formulate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 20, inf = 1e9; int h[N]; int f[N][N][N]; pair<int, int> g[N][N][N]; vector<int> ans; int main() { int n, a, b; scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]), h[i]++; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) f[i][j][k] = inf; } } f[1][0][h[1]] = 0; for (int i = 2; i < n; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) if (f[i - 1][j][k] != inf) { for (int l = 0; l < N; l++) { if (l * b >= k) { int now = h[i] - j * b - l * a; if (now < 0) now = 0; if (f[i - 1][j][k] + l < f[i][l][now]) { f[i][l][now] = f[i - 1][j][k] + l; g[i][l][now] = make_pair(j, k); } } } } } } int i = n - 1, j, k = 0, Max = inf; for (int l = 0; l < N; l++) { if (l * b >= h[n]) if (f[n - 1][l][0] < Max) { Max = f[n - 1][l][0]; j = l; } } while (i > 1) { for (int l = 0; l < j; l++) ans.push_back(i); int jj = g[i][j][k].first, kk = g[i][j][k].second; i--; j = jj; k = kk; } printf("%d\n", Max); for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]); if (ans.size()) printf("%d\n", ans[ans.size() - 1]); return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const int maxn = 110000; int n, a, b; int h[20]; vector<int> cur; vector<int> ans; void sear(int c) { if (c == n - 1) { int prev = h[c - 1]; int thiss = h[c]; int next = h[c + 1]; while (h[c - 1] >= 0 || h[c] >= 0 || h[c + 1] >= 0) { h[c - 1] -= b; h[c] -= a; h[c + 1] -= b; cur.push_back(c); } if (cur.size() < ans.size() || ans.size() == 0) ans = cur; h[c - 1] = prev; h[c] = thiss; h[c + 1] = next; while (!cur.empty()) { if (cur.back() == c) cur.pop_back(); else break; } return; } int prev = h[c - 1]; int thiss = h[c]; int next = h[c + 1]; while (h[c - 1] >= 0) { h[c - 1] -= b; h[c] -= a; h[c + 1] -= b; cur.push_back(c); } sear(c + 1); while (h[c] >= 0) { h[c] -= a; h[c + 1] -= b; cur.push_back(c); sear(c + 1); } h[c - 1] = prev; h[c] = thiss; h[c + 1] = next; while (!cur.empty()) { if (cur.back() == c) cur.pop_back(); else break; } return; } int main() { cin >> n >> a >> b; for (long long i = 1; i <= (int)n; i++) cin >> h[i]; sear(2); cout << ans.size() << endl; for (long long i = 0; i < (int)ans.size(); i++) cout << ans[i] << " "; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const int maxn = 110000; int n, a, b; int h[20]; vector<int> cur; vector<int> ans; void sear(int c) { if (c == n - 1) { int prev = h[c - 1]; int thiss = h[c]; int next = h[c + 1]; while (h[c - 1] >= 0 || h[c] >= 0 || h[c + 1] >= 0) { h[c - 1] -= b; h[c] -= a; h[c + 1] -= b; cur.push_back(c); } if (cur.size() < ans.size() || ans.size() == 0) ans = cur; h[c - 1] = prev; h[c] = thiss; h[c + 1] = next; while (!cur.empty()) { if (cur.back() == c) cur.pop_back(); else break; } return; } int prev = h[c - 1]; int thiss = h[c]; int next = h[c + 1]; while (h[c - 1] >= 0) { h[c - 1] -= b; h[c] -= a; h[c + 1] -= b; cur.push_back(c); } sear(c + 1); while (h[c] >= 0) { h[c] -= a; h[c + 1] -= b; cur.push_back(c); sear(c + 1); } h[c - 1] = prev; h[c] = thiss; h[c + 1] = next; while (!cur.empty()) { if (cur.back() == c) cur.pop_back(); else break; } return; } int main() { cin >> n >> a >> b; for (long long i = 1; i <= (int)n; i++) cin >> h[i]; sear(2); cout << ans.size() << endl; for (long long i = 0; i < (int)ans.size(); i++) cout << ans[i] << " "; return 0; } ```
#include <bits/stdc++.h> const int INF = 10000; const int N = 15; int h[N], n, a, b, ans, cnt[N], cur[N]; bool dfs(int idx, int num) { if (num > ans) return false; if (idx == n) { if (h[n] > 0) return false; ans = num; for (int i = 2; i < n; i++) { cnt[i] = cur[i]; } return true; } int hp1 = h[idx - 1]; int hp2 = h[idx]; int hp3 = h[idx + 1]; for (int i = 0; i <= 20; i++) { if (hp1 - b * i > 0) continue; if (i + num > ans) break; if (idx == n - 1 && hp3 - b * i > 0) continue; cur[idx] = i; h[idx - 1] -= b * i; h[idx] -= a * i; h[idx + 1] -= b * i; bool ok = dfs(idx + 1, num + i); h[idx - 1] += b * i; h[idx] += a * i; h[idx + 1] += b * i; if (hp2 - a * i <= 0 && hp3 - b * i <= 0) break; } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); h[i]++; } ans = INF; dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) { for (int j = 0; j < cnt[i]; j++) { printf("%d ", i); } } puts(""); return 0; }
### Prompt Please formulate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> const int INF = 10000; const int N = 15; int h[N], n, a, b, ans, cnt[N], cur[N]; bool dfs(int idx, int num) { if (num > ans) return false; if (idx == n) { if (h[n] > 0) return false; ans = num; for (int i = 2; i < n; i++) { cnt[i] = cur[i]; } return true; } int hp1 = h[idx - 1]; int hp2 = h[idx]; int hp3 = h[idx + 1]; for (int i = 0; i <= 20; i++) { if (hp1 - b * i > 0) continue; if (i + num > ans) break; if (idx == n - 1 && hp3 - b * i > 0) continue; cur[idx] = i; h[idx - 1] -= b * i; h[idx] -= a * i; h[idx + 1] -= b * i; bool ok = dfs(idx + 1, num + i); h[idx - 1] += b * i; h[idx] += a * i; h[idx + 1] += b * i; if (hp2 - a * i <= 0 && hp3 - b * i <= 0) break; } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); h[i]++; } ans = INF; dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) { for (int j = 0; j < cnt[i]; j++) { printf("%d ", i); } } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 20, oo = 100000; int n, a, b, h[MAXN], dp[MAXN][MAXN][MAXN], bk[MAXN][MAXN][MAXN]; vector<int> ans; int f(int idx, int l, int r) { if (idx == 0) return l <= 0 && r <= 0 ? 0 : oo; l = max(l, 0); r = max(r, 0); int &ans = dp[idx][l][r]; int &ph = bk[idx][l][r]; if (ans >= 0) return ans; ans = oo; int tmp = h[idx - 1]; if (r <= 0) ans = f(idx - 1, tmp, l); for (int i = 1; l > 0 || r > 0 || tmp > 0; ++i) { tmp -= b; r -= b; l -= a; if (r <= 0) { int aux = f(idx - 1, tmp, l) + i; if (aux < ans) { ans = aux; ph = i; } } } return ans; } void print(int idx, int l, int r) { l = max(l, 0); r = max(r, 0); int c = bk[idx][l][r]; if (idx == 0) return; for (int i = 0; i < c; ++i) ans.push_back(idx + 1); print(idx - 1, h[idx - 1] - c * b, l - a * c); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> a >> b; memset(dp, -1, sizeof dp); for (int i = 0; i < n; ++i) { cin >> h[i]; ++h[i]; } int val = f(n - 2, h[n - 2], h[n - 1]); print(n - 2, h[n - 2], h[n - 1]); assert(val == ans.size()); cout << val << endl; for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " \n"[i == ans.size() - 1]; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 20, oo = 100000; int n, a, b, h[MAXN], dp[MAXN][MAXN][MAXN], bk[MAXN][MAXN][MAXN]; vector<int> ans; int f(int idx, int l, int r) { if (idx == 0) return l <= 0 && r <= 0 ? 0 : oo; l = max(l, 0); r = max(r, 0); int &ans = dp[idx][l][r]; int &ph = bk[idx][l][r]; if (ans >= 0) return ans; ans = oo; int tmp = h[idx - 1]; if (r <= 0) ans = f(idx - 1, tmp, l); for (int i = 1; l > 0 || r > 0 || tmp > 0; ++i) { tmp -= b; r -= b; l -= a; if (r <= 0) { int aux = f(idx - 1, tmp, l) + i; if (aux < ans) { ans = aux; ph = i; } } } return ans; } void print(int idx, int l, int r) { l = max(l, 0); r = max(r, 0); int c = bk[idx][l][r]; if (idx == 0) return; for (int i = 0; i < c; ++i) ans.push_back(idx + 1); print(idx - 1, h[idx - 1] - c * b, l - a * c); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> a >> b; memset(dp, -1, sizeof dp); for (int i = 0; i < n; ++i) { cin >> h[i]; ++h[i]; } int val = f(n - 2, h[n - 2], h[n - 1]); print(n - 2, h[n - 2], h[n - 1]); assert(val == ans.size()); cout << val << endl; for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " \n"[i == ans.size() - 1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e1 + 5, maxm = 15 * 10 + 5; int n, a, b, h[maxn], sum, ans, all; int pos[maxm], val[maxm]; int rad() { int ret = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return ret * f; } void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; ++i) pos[++sum] = n - 1; if (ans > sum) { for (int i = 1; i <= sum; ++i) val[i] = pos[i]; ans = sum; } sum -= yu; } void put(int x) { all -= min(max(h[x - 1], -1) + a, b) + min(max(h[x], -1) + 1, a) + min(max(h[x + 1], -1) + 1, b); } void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int i = 1, lst = sum, A = h[now - 1], B = h[now], C = h[now + 1], his = all; while (true) { put(now); h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; pos[++sum] = now; if (h[now - 1] < 0) dfs(now + 1); if (h[now - 1] >= 0 || h[now] >= 0) ; else { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } if (sum >= ans) { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } } } int main() { n = rad(); a = rad(); b = rad(); for (int i = 1; i <= n; ++i) all += h[i] = rad(); ans = 1e9; sum = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i < ans; ++i) printf("%d ", val[i]); printf("%d\n", val[ans]); return 0; }
### Prompt Please create a solution in cpp to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e1 + 5, maxm = 15 * 10 + 5; int n, a, b, h[maxn], sum, ans, all; int pos[maxm], val[maxm]; int rad() { int ret = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return ret * f; } void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; ++i) pos[++sum] = n - 1; if (ans > sum) { for (int i = 1; i <= sum; ++i) val[i] = pos[i]; ans = sum; } sum -= yu; } void put(int x) { all -= min(max(h[x - 1], -1) + a, b) + min(max(h[x], -1) + 1, a) + min(max(h[x + 1], -1) + 1, b); } void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int i = 1, lst = sum, A = h[now - 1], B = h[now], C = h[now + 1], his = all; while (true) { put(now); h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; pos[++sum] = now; if (h[now - 1] < 0) dfs(now + 1); if (h[now - 1] >= 0 || h[now] >= 0) ; else { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } if (sum >= ans) { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } } } int main() { n = rad(); a = rad(); b = rad(); for (int i = 1; i <= n; ++i) all += h[i] = rad(); ans = 1e9; sum = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i < ans; ++i) printf("%d ", val[i]); printf("%d\n", val[ans]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 20; const int INF = 1e8 + 10; int n, a, b; int h[N]; int dp[N][N][N]; map<pair<int, pair<int, int> >, pair<int, pair<int, int> > > pre; map<pair<int, pair<int, int> >, int> step; vector<int> ans; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) { scanf("%d", h + i); } fill((int *)dp, (int *)(dp + N), INF); dp[1][h[1] + 1][h[2] + 1] = 0; for (int i = 2; i <= n - 1; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { if (dp[i - 1][j][k] >= INF) continue; for (int c = 0; c < N; ++c) { int nj = max(0, j - c * b); if (nj != 0) continue; int nk = max(0, k - c * a); int nn = max(0, h[i + 1] + 1 - c * b); if (dp[i][nk][nn] > dp[i - 1][j][k] + c) { pre[make_pair(i, make_pair(nk, nn))] = make_pair(i - 1, make_pair(j, k)); step[make_pair(i, make_pair(nk, nn))] = c; } dp[i][nk][nn] = min(dp[i][nk][nn], dp[i - 1][j][k] + c); } } } } printf("%d\n", dp[n - 1][0][0]); pair<int, pair<int, int> > tmp = make_pair(n - 1, make_pair(0, 0)); while (tmp.first > 1) { int c = step[tmp]; while (c--) { ans.push_back(tmp.first); } tmp = pre[tmp]; } for (int i = 0; i < ans.size(); ++i) { printf("%d%c", ans[i], " \n"[i == (int)ans.size() - 1]); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 20; const int INF = 1e8 + 10; int n, a, b; int h[N]; int dp[N][N][N]; map<pair<int, pair<int, int> >, pair<int, pair<int, int> > > pre; map<pair<int, pair<int, int> >, int> step; vector<int> ans; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) { scanf("%d", h + i); } fill((int *)dp, (int *)(dp + N), INF); dp[1][h[1] + 1][h[2] + 1] = 0; for (int i = 2; i <= n - 1; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { if (dp[i - 1][j][k] >= INF) continue; for (int c = 0; c < N; ++c) { int nj = max(0, j - c * b); if (nj != 0) continue; int nk = max(0, k - c * a); int nn = max(0, h[i + 1] + 1 - c * b); if (dp[i][nk][nn] > dp[i - 1][j][k] + c) { pre[make_pair(i, make_pair(nk, nn))] = make_pair(i - 1, make_pair(j, k)); step[make_pair(i, make_pair(nk, nn))] = c; } dp[i][nk][nn] = min(dp[i][nk][nn], dp[i - 1][j][k] + c); } } } } printf("%d\n", dp[n - 1][0][0]); pair<int, pair<int, int> > tmp = make_pair(n - 1, make_pair(0, 0)); while (tmp.first > 1) { int c = step[tmp]; while (c--) { ans.push_back(tmp.first); } tmp = pre[tmp]; } for (int i = 0; i < ans.size(); ++i) { printf("%d%c", ans[i], " \n"[i == (int)ans.size() - 1]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; int life[20]; int shotAt[155]; int N; int direct, bagalMe; bool canKill(int remainingShots, int goingToKill, int leftMostHittable) { if (remainingShots == 0) { for (int i = 1; i <= N; i++) { if (life[i] >= 0) { return false; } } return true; } if (life[goingToKill] < 0) { return canKill(remainingShots, goingToKill + 1, leftMostHittable); } if (goingToKill == N) { life[goingToKill] -= bagalMe; shotAt[remainingShots] = goingToKill - 1; if (canKill(remainingShots - 1, goingToKill, leftMostHittable)) { return true; } life[goingToKill] += bagalMe; } else { for (int shoot = max(goingToKill, leftMostHittable); shoot <= goingToKill + 1; shoot++) { if (1 < shoot and shoot < N) { life[shoot - 1] -= bagalMe; life[shoot] -= direct; life[shoot + 1] -= bagalMe; shotAt[remainingShots] = shoot; if (canKill(remainingShots - 1, goingToKill, shoot)) { return true; } life[shoot - 1] += bagalMe; life[shoot] += direct; life[shoot + 1] += bagalMe; } } } return false; } int main() { cin >> N >> direct >> bagalMe; for (int i = 1; i <= N; i++) { cin >> life[i]; } for (int ans = 1; ans < 155; ans++) { if (canKill(ans, 1, 2)) { cout << ans << endl; for (int i = ans; i > 0; i--) { cout << shotAt[i] << " "; } return 0; } } return 0; }
### Prompt Create a solution in Cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; int life[20]; int shotAt[155]; int N; int direct, bagalMe; bool canKill(int remainingShots, int goingToKill, int leftMostHittable) { if (remainingShots == 0) { for (int i = 1; i <= N; i++) { if (life[i] >= 0) { return false; } } return true; } if (life[goingToKill] < 0) { return canKill(remainingShots, goingToKill + 1, leftMostHittable); } if (goingToKill == N) { life[goingToKill] -= bagalMe; shotAt[remainingShots] = goingToKill - 1; if (canKill(remainingShots - 1, goingToKill, leftMostHittable)) { return true; } life[goingToKill] += bagalMe; } else { for (int shoot = max(goingToKill, leftMostHittable); shoot <= goingToKill + 1; shoot++) { if (1 < shoot and shoot < N) { life[shoot - 1] -= bagalMe; life[shoot] -= direct; life[shoot + 1] -= bagalMe; shotAt[remainingShots] = shoot; if (canKill(remainingShots - 1, goingToKill, shoot)) { return true; } life[shoot - 1] += bagalMe; life[shoot] += direct; life[shoot + 1] += bagalMe; } } } return false; } int main() { cin >> N >> direct >> bagalMe; for (int i = 1; i <= N; i++) { cin >> life[i]; } for (int ans = 1; ans < 155; ans++) { if (canKill(ans, 1, 2)) { cout << ans << endl; for (int i = ans; i > 0; i--) { cout << shotAt[i] << " "; } return 0; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const double eps = 1e-8; const double PIE = acos(-1.0); const int dx[] = {0, -1, 0, 1}; const int dy[] = {1, 0, -1, 0}; const int fx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int fy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; void openfile() { freopen("data.in", "rb", stdin); freopen("data.out", "wb", stdout); } int n, a, b; int h[15]; int ans, temp[100], path[100]; int check(int x) { if (x <= 0) return 0; else return (x - 1) / b + 1; } int check2(int x, int y) { x = x <= 0 ? 0 : (x - 1) / a + 1; y = y <= 0 ? 0 : (y - 1) / b + 1; return x > y ? x : y; } void dfs(int num, int cnt) { if (cnt >= ans) return; if (num >= n) { int i; for (i = 2; i <= n - 1; i++) if (h[i] > 0) return; if (cnt < ans) { ans = cnt; for (i = 1; i <= n; i++) temp[i] = path[i]; return; } } for (int i = 2; i <= num - 2; i++) if (h[i] > 0) return; for (int i = check(h[num - 1]); i <= check2(h[num], h[num + 1]); i++) { h[num] -= i * a; h[num - 1] -= i * b; h[num + 1] -= i * b; path[num] = i; dfs(num + 1, cnt + i); h[num] += i * a; h[num - 1] += i * b; h[num + 1] += i * b; path[num] = -1; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = (1); i <= (n); i++) { scanf("%d", &h[i]); h[i]++; } int cnta = 0, cntb = 0; while (h[1] > 0) { h[2] -= a; h[3] -= b; h[1] -= b; cnta++; } while (h[n] > 0) { h[n - 1] -= a; h[n] -= b; h[n - 2] -= b; cntb++; } memset(path, -1, sizeof(path)); ans = INF; for (int i = (2); i <= (n - 1); i++) { if (h[i] > 0) { dfs(i, 0); break; } } if (ans == INF) printf("%d\n", cnta + cntb); else printf("%d\n", cnta + cntb + ans); for (int i = 0; i < cnta; i++) printf("%d ", 2); for (int i = 0; i < cntb; i++) printf("%d ", n - 1); if (ans != INF) for (int i = 1; i <= n; i++) if (temp[i] != -1) { for (int j = 0; j < temp[i]; j++) printf("%d ", i); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const double eps = 1e-8; const double PIE = acos(-1.0); const int dx[] = {0, -1, 0, 1}; const int dy[] = {1, 0, -1, 0}; const int fx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int fy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; void openfile() { freopen("data.in", "rb", stdin); freopen("data.out", "wb", stdout); } int n, a, b; int h[15]; int ans, temp[100], path[100]; int check(int x) { if (x <= 0) return 0; else return (x - 1) / b + 1; } int check2(int x, int y) { x = x <= 0 ? 0 : (x - 1) / a + 1; y = y <= 0 ? 0 : (y - 1) / b + 1; return x > y ? x : y; } void dfs(int num, int cnt) { if (cnt >= ans) return; if (num >= n) { int i; for (i = 2; i <= n - 1; i++) if (h[i] > 0) return; if (cnt < ans) { ans = cnt; for (i = 1; i <= n; i++) temp[i] = path[i]; return; } } for (int i = 2; i <= num - 2; i++) if (h[i] > 0) return; for (int i = check(h[num - 1]); i <= check2(h[num], h[num + 1]); i++) { h[num] -= i * a; h[num - 1] -= i * b; h[num + 1] -= i * b; path[num] = i; dfs(num + 1, cnt + i); h[num] += i * a; h[num - 1] += i * b; h[num + 1] += i * b; path[num] = -1; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = (1); i <= (n); i++) { scanf("%d", &h[i]); h[i]++; } int cnta = 0, cntb = 0; while (h[1] > 0) { h[2] -= a; h[3] -= b; h[1] -= b; cnta++; } while (h[n] > 0) { h[n - 1] -= a; h[n] -= b; h[n - 2] -= b; cntb++; } memset(path, -1, sizeof(path)); ans = INF; for (int i = (2); i <= (n - 1); i++) { if (h[i] > 0) { dfs(i, 0); break; } } if (ans == INF) printf("%d\n", cnta + cntb); else printf("%d\n", cnta + cntb + ans); for (int i = 0; i < cnta; i++) printf("%d ", 2); for (int i = 0; i < cntb; i++) printf("%d ", n - 1); if (ans != INF) for (int i = 1; i <= n; i++) if (temp[i] != -1) { for (int j = 0; j < temp[i]; j++) printf("%d ", i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int fireball1, fireball2, health[100], attack[100], attack1[100]; int n, sum = 2e+8; void dfs(int x, int y) { if (y >= sum) return; if (x == n) { sum = y; for (int i = 1; i <= n; i++) attack[i] = attack1[i]; return; } for (int i = 0; i <= 100; i++) { if (y + i > sum) break; if (health[x - 1] - fireball2 * i >= 0) continue; if (x == n - 1) if (health[n] - fireball2 * i >= 0) continue; attack1[x] = i; health[x - 1] -= fireball2 * i; health[x] -= fireball1 * i; health[x + 1] -= fireball2 * i; dfs(x + 1, y + i); health[x - 1] += fireball2 * i; health[x] += fireball1 * i; health[x + 1] += fireball2 * i; if (health[x] - fireball1 * i < 0 && health[x + 1] - fireball2 * i < 0) break; } } int main() { scanf("%d%d%d", &n, &fireball1, &fireball2); for (int i = 1; i <= n; i++) scanf("%d", &health[i]); dfs(2, 0); printf("%d\n", sum); for (int i = 2; i < n; i++) for (int j = 1; j <= attack[i]; j++) printf("%d ", i); }
### Prompt Please formulate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int fireball1, fireball2, health[100], attack[100], attack1[100]; int n, sum = 2e+8; void dfs(int x, int y) { if (y >= sum) return; if (x == n) { sum = y; for (int i = 1; i <= n; i++) attack[i] = attack1[i]; return; } for (int i = 0; i <= 100; i++) { if (y + i > sum) break; if (health[x - 1] - fireball2 * i >= 0) continue; if (x == n - 1) if (health[n] - fireball2 * i >= 0) continue; attack1[x] = i; health[x - 1] -= fireball2 * i; health[x] -= fireball1 * i; health[x + 1] -= fireball2 * i; dfs(x + 1, y + i); health[x - 1] += fireball2 * i; health[x] += fireball1 * i; health[x + 1] += fireball2 * i; if (health[x] - fireball1 * i < 0 && health[x + 1] - fireball2 * i < 0) break; } } int main() { scanf("%d%d%d", &n, &fireball1, &fireball2); for (int i = 1; i <= n; i++) scanf("%d", &health[i]); dfs(2, 0); printf("%d\n", sum); for (int i = 2; i < n; i++) for (int j = 1; j <= attack[i]; j++) printf("%d ", i); } ```
#include <bits/stdc++.h> using namespace std; int maps[15]; int ans = 99999; int a, b; int n; int fireans[15]; int firetmp[15]; void dfs(int now, int times) { if (times >= ans) return; if (now == n - 1) { int firetime = (int)max(max(maps[now - 1], maps[now + 1]) / b, maps[now] / a) + 1; if (times + firetime < ans) { ans = times + firetime; for (int i = 0; i < 15; i++) { fireans[i] = firetmp[i]; } fireans[n - 1] += firetime; } return; } if (maps[now - 1] >= 0) { int firetime = maps[now - 1] / b + 1; maps[now - 1] -= firetime * b; maps[now] -= firetime * a; maps[now + 1] -= firetime * b; firetmp[now] += firetime; dfs(now, times + firetime); firetmp[now] -= firetime; maps[now - 1] += firetime * b; maps[now] += firetime * a; maps[now + 1] += firetime * b; } else { { int firetime = max(max(maps[now - 1], maps[now + 1]) / b, maps[now] / a) + 1; for (int i = 0; i <= firetime; i++) { maps[now - 1] -= i * b; maps[now] -= i * a; maps[now + 1] -= i * b; firetmp[now] += i; dfs(now + 1, times + i); firetmp[now] -= i; maps[now - 1] += i * b; maps[now] += i * a; maps[now + 1] += i * b; } } } } int main() { cin >> n >> a >> b; for (int i = 1; i <= n; i++) { cin >> maps[i]; } dfs(2, 0); cout << ans << endl; vector<int> VI; VI.clear(); for (int i = 0; i < 15; i++) { for (int j = 0; j < fireans[i]; j++) VI.push_back(i); } cout << VI[0]; for (int i = 1; i < VI.size(); i++) { cout << " " << VI[i]; } cout << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int maps[15]; int ans = 99999; int a, b; int n; int fireans[15]; int firetmp[15]; void dfs(int now, int times) { if (times >= ans) return; if (now == n - 1) { int firetime = (int)max(max(maps[now - 1], maps[now + 1]) / b, maps[now] / a) + 1; if (times + firetime < ans) { ans = times + firetime; for (int i = 0; i < 15; i++) { fireans[i] = firetmp[i]; } fireans[n - 1] += firetime; } return; } if (maps[now - 1] >= 0) { int firetime = maps[now - 1] / b + 1; maps[now - 1] -= firetime * b; maps[now] -= firetime * a; maps[now + 1] -= firetime * b; firetmp[now] += firetime; dfs(now, times + firetime); firetmp[now] -= firetime; maps[now - 1] += firetime * b; maps[now] += firetime * a; maps[now + 1] += firetime * b; } else { { int firetime = max(max(maps[now - 1], maps[now + 1]) / b, maps[now] / a) + 1; for (int i = 0; i <= firetime; i++) { maps[now - 1] -= i * b; maps[now] -= i * a; maps[now + 1] -= i * b; firetmp[now] += i; dfs(now + 1, times + i); firetmp[now] -= i; maps[now - 1] += i * b; maps[now] += i * a; maps[now + 1] += i * b; } } } } int main() { cin >> n >> a >> b; for (int i = 1; i <= n; i++) { cin >> maps[i]; } dfs(2, 0); cout << ans << endl; vector<int> VI; VI.clear(); for (int i = 0; i < 15; i++) { for (int j = 0; j < fireans[i]; j++) VI.push_back(i); } cout << VI[0]; for (int i = 1; i < VI.size(); i++) { cout << " " << VI[i]; } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { int x, y, k; } pre[20][20][20], ans, now; int anss = 10000000, a, b, n, tot; int dp[20][20][20]; int aa[1000]; int p[1000]; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &aa[i]); aa[i]++; } for (int i = 0; i <= n; i++) for (int j = 0; j <= 16; j++) for (int k = 0; k <= 16; k++) dp[i][j][k] = 151515141; dp[1][aa[1]][aa[2]] = 0; for (int i = 1; i <= n - 2; i++) for (int j = 0; j <= 16; j++) for (int k = 0; k <= 16; k++) for (int l = (j + b - 1) / b; l <= 16; l++) if (dp[i][j][k] < 100000) { int nj = max(k - l * a, 0); int nk = max(aa[i + 2] - l * b, 0); if (dp[i + 1][nj][nk] > (dp[i][j][k] + l)) { pre[i + 1][nj][nk] = (node){j, k, l}; dp[i + 1][nj][nk] = dp[i][j][k] + l; } } for (int i = 0; i <= 16; i++) for (int j = 0; j <= 16; j++) { if (anss > (dp[n - 1][i][j] + max((i - 1 + a) / a, (b + j - 1) / b))) { ans = (node){i, j, max((i - 1 + a) / a, (j - 1 + b) / b)}; anss = min(anss, dp[n - 1][i][j] + max((i - 1 + a) / a, (b + j - 1) / b)); } } cout << anss << endl; now = pre[n - 1][ans.x][ans.y]; for (int i = n - 2; i >= 1; i--) { for (int j = 1; j <= now.k; j++) p[++tot] = i + 1; now = pre[i][now.x][now.y]; } for (int i = 1; i <= ans.k; i++) p[++tot] = n - 1; for (int i = tot; (i); i--) printf("%d ", p[i]); }
### Prompt Your task is to create a cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int x, y, k; } pre[20][20][20], ans, now; int anss = 10000000, a, b, n, tot; int dp[20][20][20]; int aa[1000]; int p[1000]; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &aa[i]); aa[i]++; } for (int i = 0; i <= n; i++) for (int j = 0; j <= 16; j++) for (int k = 0; k <= 16; k++) dp[i][j][k] = 151515141; dp[1][aa[1]][aa[2]] = 0; for (int i = 1; i <= n - 2; i++) for (int j = 0; j <= 16; j++) for (int k = 0; k <= 16; k++) for (int l = (j + b - 1) / b; l <= 16; l++) if (dp[i][j][k] < 100000) { int nj = max(k - l * a, 0); int nk = max(aa[i + 2] - l * b, 0); if (dp[i + 1][nj][nk] > (dp[i][j][k] + l)) { pre[i + 1][nj][nk] = (node){j, k, l}; dp[i + 1][nj][nk] = dp[i][j][k] + l; } } for (int i = 0; i <= 16; i++) for (int j = 0; j <= 16; j++) { if (anss > (dp[n - 1][i][j] + max((i - 1 + a) / a, (b + j - 1) / b))) { ans = (node){i, j, max((i - 1 + a) / a, (j - 1 + b) / b)}; anss = min(anss, dp[n - 1][i][j] + max((i - 1 + a) / a, (b + j - 1) / b)); } } cout << anss << endl; now = pre[n - 1][ans.x][ans.y]; for (int i = n - 2; i >= 1; i--) { for (int j = 1; j <= now.k; j++) p[++tot] = i + 1; now = pre[i][now.x][now.y]; } for (int i = 1; i <= ans.k; i++) p[++tot] = n - 1; for (int i = tot; (i); i--) printf("%d ", p[i]); } ```
#include <bits/stdc++.h> using namespace std; template <class T> void dbs(string str, T t) { cerr << str << " : " << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { long long idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...); } template <class T> void prc(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << *i; } cerr << "]\n"; } template <class T> void prall(T a) { prc(a.begin(), a.end()); } const int inf = 1000 * 1000 * 1000 + 9; const long long infl = 1ll * inf * inf; int mod = 1000 * 1000 * 1000 + 7; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); long long randrange(long long l, long long r) { return l + mt() % (r - l + 1); } int h[15]; int dp[12][800][800]; int off = 400; int n, a, b; int kill(int i, int prevrem, int currem) { if (dp[i][prevrem + off][currem + off] != -1) return dp[i][prevrem + off][currem + off]; int shots = inf; if (i == n) { if (prevrem < 0 && currem < 0) shots = 0; } else { for (int j = 0; j <= 16; j++) { if (prevrem - b * j >= 0) continue; shots = min(shots, kill(i + 1, currem - a * j, h[i + 1] - b * j) + j); } } return dp[i][prevrem + off][currem + off] = shots; } void printsol(int i, int prevrem, int currem) { if (i == n) { cout << '\n'; } else { for (int j = 0; j <= 16; j++) { if (prevrem - b * j >= 0) continue; if (dp[i][prevrem + off][currem + off] == j + dp[i + 1][currem - a * j + off][h[i + 1] - b * j + off]) { for (int k = 0; k < j; k++) cout << i << " "; printsol(i + 1, currem - a * j, h[i + 1] - b * j); break; } } } } void solve() { cin >> n >> a >> b; memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; i++) { cin >> h[i]; } int t = kill(2, h[1], h[2]); cout << t << '\n'; printsol(2, h[1], h[2]); } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); int testcases = 1; cout << fixed << setprecision(13); int cs = 1; while (testcases--) { cs++; solve(); } }
### Prompt Create a solution in CPP for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> void dbs(string str, T t) { cerr << str << " : " << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { long long idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...); } template <class T> void prc(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << *i; } cerr << "]\n"; } template <class T> void prall(T a) { prc(a.begin(), a.end()); } const int inf = 1000 * 1000 * 1000 + 9; const long long infl = 1ll * inf * inf; int mod = 1000 * 1000 * 1000 + 7; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); long long randrange(long long l, long long r) { return l + mt() % (r - l + 1); } int h[15]; int dp[12][800][800]; int off = 400; int n, a, b; int kill(int i, int prevrem, int currem) { if (dp[i][prevrem + off][currem + off] != -1) return dp[i][prevrem + off][currem + off]; int shots = inf; if (i == n) { if (prevrem < 0 && currem < 0) shots = 0; } else { for (int j = 0; j <= 16; j++) { if (prevrem - b * j >= 0) continue; shots = min(shots, kill(i + 1, currem - a * j, h[i + 1] - b * j) + j); } } return dp[i][prevrem + off][currem + off] = shots; } void printsol(int i, int prevrem, int currem) { if (i == n) { cout << '\n'; } else { for (int j = 0; j <= 16; j++) { if (prevrem - b * j >= 0) continue; if (dp[i][prevrem + off][currem + off] == j + dp[i + 1][currem - a * j + off][h[i + 1] - b * j + off]) { for (int k = 0; k < j; k++) cout << i << " "; printsol(i + 1, currem - a * j, h[i + 1] - b * j); break; } } } } void solve() { cin >> n >> a >> b; memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; i++) { cin >> h[i]; } int t = kill(2, h[1], h[2]); cout << t << '\n'; printsol(2, h[1], h[2]); } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); int testcases = 1; cout << fixed << setprecision(13); int cs = 1; while (testcases--) { cs++; solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 15; int n, a, b, h[MAXN]; int ans = 150, c[MAXN], res[MAXN], buf[MAXN]; void dfs(int now) { if (now > n - 1) { if (h[now - 1] < 0) { int sum = 0; for (int i = 2; i <= n - 1; i++) sum += c[i]; if (sum < ans) { ans = sum; for (int i = 2; i <= n - 1; i++) res[i] = c[i]; } } return; } for (int i = 8; i >= 0 && i * b > h[now - 1]; i--) { c[now] = i; h[now] -= i * a; h[now + 1] -= i * b; dfs(now + 1); h[now] += i * a; h[now + 1] += i * b; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); buf[2] = max(buf[2], h[1] / b + 1); buf[n - 1] = max(buf[n - 1], h[n] / b + 1); h[1] -= b * buf[2], h[2] -= a * buf[2], h[3] -= b * buf[2]; if (n > 3) h[n - 2] -= b * buf[n - 1], h[n - 1] -= a * buf[n - 1], h[n] -= b * buf[n - 1]; dfs(2); printf("%d\n", ans + buf[2] + (n > 3) * buf[n - 1]); for (int i = 2; i <= n - 1; i++) for (int j = 0; j < res[i] + buf[i]; j++) printf("%d ", i); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 15; int n, a, b, h[MAXN]; int ans = 150, c[MAXN], res[MAXN], buf[MAXN]; void dfs(int now) { if (now > n - 1) { if (h[now - 1] < 0) { int sum = 0; for (int i = 2; i <= n - 1; i++) sum += c[i]; if (sum < ans) { ans = sum; for (int i = 2; i <= n - 1; i++) res[i] = c[i]; } } return; } for (int i = 8; i >= 0 && i * b > h[now - 1]; i--) { c[now] = i; h[now] -= i * a; h[now + 1] -= i * b; dfs(now + 1); h[now] += i * a; h[now + 1] += i * b; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); buf[2] = max(buf[2], h[1] / b + 1); buf[n - 1] = max(buf[n - 1], h[n] / b + 1); h[1] -= b * buf[2], h[2] -= a * buf[2], h[3] -= b * buf[2]; if (n > 3) h[n - 2] -= b * buf[n - 1], h[n - 1] -= a * buf[n - 1], h[n] -= b * buf[n - 1]; dfs(2); printf("%d\n", ans + buf[2] + (n > 3) * buf[n - 1]); for (int i = 2; i <= n - 1; i++) for (int j = 0; j < res[i] + buf[i]; j++) printf("%d ", i); return 0; } ```
#include <bits/stdc++.h> using namespace std; int h[20]; int dp[20][20][20]; int cntL[20][20][20], cntM[20][20][20]; vector<int> v; int main() { int i, j, k, n, a, b, l, m; cin >> n >> a >> b; for (i = 0; i < n; i++) cin >> h[i]; int cnt = 0; while (h[0] >= 0) { h[0] -= b; h[1] -= a; h[2] -= b; v.push_back(2); cnt++; } while (h[n - 1] >= 0) { h[n - 1] -= b; h[n - 2] -= a; h[n - 3] -= b; v.push_back(n - 1); cnt++; } for (i = 1; i <= n - 2; i++) { if (h[i] >= 0) h[i]++; } for (i = 0; i <= n - 2; i++) for (j = 0; j <= 16; j++) for (k = 0; k <= 16; k++) dp[i][j][k] = INT_MAX; dp[0][0][0] = 0; for (i = 1; i <= n - 2; i++) for (j = 0; j <= 16; j++) { for (l = 0; l <= 16; l++) for (m = 0; m <= 16; m++) { if (dp[i - 1][l][m] != INT_MAX && l - j * b <= 0) { int lft = max(0, h[i] - m * b - a * j); if (dp[i][lft][j] > dp[i - 1][l][m] + j) { dp[i][lft][j] = dp[i - 1][l][m] + j; cntL[i][lft][j] = l; cntM[i][lft][j] = m; } } } } int ans = INT_MAX; int idxL = -1, idxM = -1; for (i = 0; i <= 16; i++) { if (ans > dp[n - 2][0][i]) { ans = dp[n - 2][0][i]; idxL = 0; idxM = i; } } for (i = n - 2; i > 0; i--) { for (j = 0; j < idxM; j++) { v.push_back(i + 1); } int tIdxL = cntL[i][idxL][idxM]; int tIdxM = cntM[i][idxL][idxM]; idxL = tIdxL; idxM = tIdxM; } cout << ans + cnt << endl; for (i = 0; i < v.size(); i++) cout << v[i] << " "; }
### Prompt Your task is to create a cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int h[20]; int dp[20][20][20]; int cntL[20][20][20], cntM[20][20][20]; vector<int> v; int main() { int i, j, k, n, a, b, l, m; cin >> n >> a >> b; for (i = 0; i < n; i++) cin >> h[i]; int cnt = 0; while (h[0] >= 0) { h[0] -= b; h[1] -= a; h[2] -= b; v.push_back(2); cnt++; } while (h[n - 1] >= 0) { h[n - 1] -= b; h[n - 2] -= a; h[n - 3] -= b; v.push_back(n - 1); cnt++; } for (i = 1; i <= n - 2; i++) { if (h[i] >= 0) h[i]++; } for (i = 0; i <= n - 2; i++) for (j = 0; j <= 16; j++) for (k = 0; k <= 16; k++) dp[i][j][k] = INT_MAX; dp[0][0][0] = 0; for (i = 1; i <= n - 2; i++) for (j = 0; j <= 16; j++) { for (l = 0; l <= 16; l++) for (m = 0; m <= 16; m++) { if (dp[i - 1][l][m] != INT_MAX && l - j * b <= 0) { int lft = max(0, h[i] - m * b - a * j); if (dp[i][lft][j] > dp[i - 1][l][m] + j) { dp[i][lft][j] = dp[i - 1][l][m] + j; cntL[i][lft][j] = l; cntM[i][lft][j] = m; } } } } int ans = INT_MAX; int idxL = -1, idxM = -1; for (i = 0; i <= 16; i++) { if (ans > dp[n - 2][0][i]) { ans = dp[n - 2][0][i]; idxL = 0; idxM = i; } } for (i = n - 2; i > 0; i--) { for (j = 0; j < idxM; j++) { v.push_back(i + 1); } int tIdxL = cntL[i][idxL][idxM]; int tIdxM = cntM[i][idxL][idxM]; idxL = tIdxL; idxM = tIdxM; } cout << ans + cnt << endl; for (i = 0; i < v.size(); i++) cout << v[i] << " "; } ```
#include <bits/stdc++.h> using namespace std; const int N = 50; int a[N], A, B; int n, dp[N][N][N]; pair<int, int> to[N][N][N]; int calcdp(int r, int sr1, int sr2) { int &res = dp[r][sr1][sr2]; if (res != -1) return res; res = 1e9; int ar = a[r] - sr1 * B; int ar1 = a[r + 1] - sr1 * A - sr2 * B; if (r == 0 && ar1 < 0) return res = 0; if (ar1 >= 0 && r + 1 != n - 1) { int d = calcdp(r, sr1 + 1, sr2) + 1; if (d < res) { to[r][sr1][sr2] = make_pair(r + 1, 1); res = d; } } if (r > 0) { int sr = 0; if (ar1 >= 0) sr = ar1 / B + 1; ar -= sr * A; int d = calcdp(r - 1, sr, sr1) + sr; if (d < res) { to[r][sr1][sr2] = make_pair(r, sr); res = d; } } return res; } bool read() { if (!(cin >> n >> A >> B)) return false; for (int i = 0; i < int(n); ++i) cin >> a[i]; return true; } vector<int> res; void restore(int r, int sr1, int sr2) { if (calcdp(r, sr1, sr2) == 0) return; pair<int, int> p = to[r][sr1][sr2]; for (int i = 0; i < int(p.second); ++i) res.push_back(p.first); if (p.first == r) { restore(r - 1, p.second, sr1); } else { assert(p.first == r + 1); restore(r, sr1 + p.second, sr2); } } void solve() { memset(dp, -1, sizeof dp); res = vector<int>(); while (a[0] >= 0) { res.push_back(1); a[0] -= B; a[1] -= A; a[2] -= B; } restore(n - 2, 0, 0); cout << int(res.size()) << endl; for (int i = 0; i < int(int(res.size())); ++i) cout << res[i] + 1 << " "; puts(""); } int main() { while (read()) solve(); return 0; }
### Prompt In Cpp, your task is to solve the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 50; int a[N], A, B; int n, dp[N][N][N]; pair<int, int> to[N][N][N]; int calcdp(int r, int sr1, int sr2) { int &res = dp[r][sr1][sr2]; if (res != -1) return res; res = 1e9; int ar = a[r] - sr1 * B; int ar1 = a[r + 1] - sr1 * A - sr2 * B; if (r == 0 && ar1 < 0) return res = 0; if (ar1 >= 0 && r + 1 != n - 1) { int d = calcdp(r, sr1 + 1, sr2) + 1; if (d < res) { to[r][sr1][sr2] = make_pair(r + 1, 1); res = d; } } if (r > 0) { int sr = 0; if (ar1 >= 0) sr = ar1 / B + 1; ar -= sr * A; int d = calcdp(r - 1, sr, sr1) + sr; if (d < res) { to[r][sr1][sr2] = make_pair(r, sr); res = d; } } return res; } bool read() { if (!(cin >> n >> A >> B)) return false; for (int i = 0; i < int(n); ++i) cin >> a[i]; return true; } vector<int> res; void restore(int r, int sr1, int sr2) { if (calcdp(r, sr1, sr2) == 0) return; pair<int, int> p = to[r][sr1][sr2]; for (int i = 0; i < int(p.second); ++i) res.push_back(p.first); if (p.first == r) { restore(r - 1, p.second, sr1); } else { assert(p.first == r + 1); restore(r, sr1 + p.second, sr2); } } void solve() { memset(dp, -1, sizeof dp); res = vector<int>(); while (a[0] >= 0) { res.push_back(1); a[0] -= B; a[1] -= A; a[2] -= B; } restore(n - 2, 0, 0); cout << int(res.size()) << endl; for (int i = 0; i < int(int(res.size())); ++i) cout << res[i] + 1 << " "; puts(""); } int main() { while (read()) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = ((res % p) * (x % p)) % p; y = y >> 1; x = ((x % p) * (x % p)) % p; } return res; } long long raichu(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; } bool isprime(long long n) { if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; else { long long z = sqrt(n); for (int i = 0; i < z - 1; i++) if (n % (i + 2) == 0) return false; return true; } } pair<int, vector<int> > dp[20][20][50]; pair<int, vector<int> > solve(vector<int> &v, int pos, int a, int b, int demand) { if (pos == 0 && demand > 0) return make_pair(1e9, vector<int>(10, 1e8)); else if (pos == 0) return make_pair(0, vector<int>(10, 0)); else if (dp[pos][demand][v[pos] + 20].first != -1) return dp[pos][demand][v[pos] + 20]; else { vector<int> res(10, 0); vector<int> temp; v[pos] -= (a * demand); v[pos + 1] -= (b * demand); v[pos - 1] -= (b * demand); int cnt = 0; for (int i = 0; i < demand; i++) res[pos]++; int ans = 1e9; while (v[pos] >= 0) { int cur = v[pos], chk = 0; while (cur >= 0) { cur -= b; chk++; } pair<int, vector<int> > z = solve(v, pos - 1, a, b, chk); if (z.first + cnt + demand <= ans) { ans = z.first + cnt + demand; temp = z.second; for (int i = 0; i < cnt; i++) temp[pos]++; } v[pos] -= a; v[pos + 1] -= b; v[pos - 1] -= b; cnt++; } pair<int, vector<int> > z = solve(v, pos - 1, a, b, 0); if (z.first + cnt + demand <= ans) { ans = z.first + cnt + demand; temp = z.second; for (int i = 0; i < cnt; i++) temp[pos]++; } v[pos] += (a * (cnt + demand)); v[pos + 1] += (b * (cnt + demand)); v[pos - 1] += (b * (cnt + demand)); for (int i = 0; i < 10; i++) res[i] += temp[i]; dp[pos][demand][v[pos] + 20] = make_pair(ans, res); return dp[pos][demand][v[pos] + 20]; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1, T; T = t; while (t--) { int n, a, b; cin >> n >> a >> b; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; int cnt = 0; vector<int> ans(10, 0); while (h[0] >= 0) { ans[1]++; cnt++; h[0] -= b; h[1] -= a; h[2] -= b; } while (h[n - 1] >= 0) { ans[n - 2]++; cnt++; h[n - 3] -= b; h[n - 2] -= a; h[n - 1] -= b; } vector<int> v = h; for (int i = 0; i < 20; i++) for (int j = 0; j < 20; j++) for (int k = 0; k < 50; k++) dp[i][j][k].first = -1; pair<int, vector<int> > z = solve(v, n - 2, a, b, 0); cout << z.first + cnt << endl; for (int i = 0; i < 10; i++) ans[i] += z.second[i]; for (int i = 0; i < 10; i++) for (int j = 0; j < ans[i]; j++) cout << i + 1 << " "; } return 0; }
### Prompt Create a solution in Cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = ((res % p) * (x % p)) % p; y = y >> 1; x = ((x % p) * (x % p)) % p; } return res; } long long raichu(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; } bool isprime(long long n) { if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; else { long long z = sqrt(n); for (int i = 0; i < z - 1; i++) if (n % (i + 2) == 0) return false; return true; } } pair<int, vector<int> > dp[20][20][50]; pair<int, vector<int> > solve(vector<int> &v, int pos, int a, int b, int demand) { if (pos == 0 && demand > 0) return make_pair(1e9, vector<int>(10, 1e8)); else if (pos == 0) return make_pair(0, vector<int>(10, 0)); else if (dp[pos][demand][v[pos] + 20].first != -1) return dp[pos][demand][v[pos] + 20]; else { vector<int> res(10, 0); vector<int> temp; v[pos] -= (a * demand); v[pos + 1] -= (b * demand); v[pos - 1] -= (b * demand); int cnt = 0; for (int i = 0; i < demand; i++) res[pos]++; int ans = 1e9; while (v[pos] >= 0) { int cur = v[pos], chk = 0; while (cur >= 0) { cur -= b; chk++; } pair<int, vector<int> > z = solve(v, pos - 1, a, b, chk); if (z.first + cnt + demand <= ans) { ans = z.first + cnt + demand; temp = z.second; for (int i = 0; i < cnt; i++) temp[pos]++; } v[pos] -= a; v[pos + 1] -= b; v[pos - 1] -= b; cnt++; } pair<int, vector<int> > z = solve(v, pos - 1, a, b, 0); if (z.first + cnt + demand <= ans) { ans = z.first + cnt + demand; temp = z.second; for (int i = 0; i < cnt; i++) temp[pos]++; } v[pos] += (a * (cnt + demand)); v[pos + 1] += (b * (cnt + demand)); v[pos - 1] += (b * (cnt + demand)); for (int i = 0; i < 10; i++) res[i] += temp[i]; dp[pos][demand][v[pos] + 20] = make_pair(ans, res); return dp[pos][demand][v[pos] + 20]; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1, T; T = t; while (t--) { int n, a, b; cin >> n >> a >> b; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; int cnt = 0; vector<int> ans(10, 0); while (h[0] >= 0) { ans[1]++; cnt++; h[0] -= b; h[1] -= a; h[2] -= b; } while (h[n - 1] >= 0) { ans[n - 2]++; cnt++; h[n - 3] -= b; h[n - 2] -= a; h[n - 1] -= b; } vector<int> v = h; for (int i = 0; i < 20; i++) for (int j = 0; j < 20; j++) for (int k = 0; k < 50; k++) dp[i][j][k].first = -1; pair<int, vector<int> > z = solve(v, n - 2, a, b, 0); cout << z.first + cnt << endl; for (int i = 0; i < 10; i++) ans[i] += z.second[i]; for (int i = 0; i < 10; i++) for (int j = 0; j < ans[i]; j++) cout << i + 1 << " "; } return 0; } ```
#include <bits/stdc++.h> inline int cmp(double x, double y = 0, double tol = 1e-9) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int n, a, b; int health[20]; int damage[20]; int mem[20][20][20]; int times_at[20][20][20]; int kill(int pos, int power) { if (health[pos] >= damage[pos]) return (health[pos] - damage[pos]) / power + 1; return 0; } int solve(int pos) { if (mem[pos][damage[pos - 1]][damage[pos]] != -1) return mem[pos][damage[pos - 1]][damage[pos]]; int min = 0, max = 0; if (pos == n - 1) { max = std::max(kill(pos - 1, b), kill(pos + 1, b)); max = std::max(kill(pos, a), max); return mem[pos][damage[pos - 1]][damage[pos]] = times_at[pos][damage[pos - 1]][damage[pos]] = max; } min = kill(pos - 1, b); max = std::max(kill(pos, a), kill(pos + 1, b)); max = std::max(min, max); int aux, best_times, best = 500; for (int times = min; times <= max; ++times) { damage[pos - 1] += times * b; damage[pos + 1] += times * b; damage[pos] += times * a; aux = times + solve(pos + 1); if (aux < best) { best = aux; best_times = times; } damage[pos - 1] -= times * b; damage[pos + 1] -= times * b; damage[pos] -= times * a; } mem[pos][damage[pos - 1]][damage[pos]] = best; times_at[pos][damage[pos - 1]][damage[pos]] = best_times; return best; } void build_path(int pos) { if (pos == n) { std::cout << std::endl; return; } int times = times_at[pos][damage[pos - 1]][damage[pos]]; for (int i = 0; i < times; ++i) std::cout << pos << " "; damage[pos - 1] += times * b; damage[pos + 1] += times * b; damage[pos] += times * a; build_path(pos + 1); } int main() { memset(mem, -1, sizeof(mem)); std::cin >> n >> a >> b; for (int i = 1; i <= n; ++i) std::cin >> health[i]; std::cout << solve(2) << std::endl; build_path(2); return 0; }
### Prompt Please formulate a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> inline int cmp(double x, double y = 0, double tol = 1e-9) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int n, a, b; int health[20]; int damage[20]; int mem[20][20][20]; int times_at[20][20][20]; int kill(int pos, int power) { if (health[pos] >= damage[pos]) return (health[pos] - damage[pos]) / power + 1; return 0; } int solve(int pos) { if (mem[pos][damage[pos - 1]][damage[pos]] != -1) return mem[pos][damage[pos - 1]][damage[pos]]; int min = 0, max = 0; if (pos == n - 1) { max = std::max(kill(pos - 1, b), kill(pos + 1, b)); max = std::max(kill(pos, a), max); return mem[pos][damage[pos - 1]][damage[pos]] = times_at[pos][damage[pos - 1]][damage[pos]] = max; } min = kill(pos - 1, b); max = std::max(kill(pos, a), kill(pos + 1, b)); max = std::max(min, max); int aux, best_times, best = 500; for (int times = min; times <= max; ++times) { damage[pos - 1] += times * b; damage[pos + 1] += times * b; damage[pos] += times * a; aux = times + solve(pos + 1); if (aux < best) { best = aux; best_times = times; } damage[pos - 1] -= times * b; damage[pos + 1] -= times * b; damage[pos] -= times * a; } mem[pos][damage[pos - 1]][damage[pos]] = best; times_at[pos][damage[pos - 1]][damage[pos]] = best_times; return best; } void build_path(int pos) { if (pos == n) { std::cout << std::endl; return; } int times = times_at[pos][damage[pos - 1]][damage[pos]]; for (int i = 0; i < times; ++i) std::cout << pos << " "; damage[pos - 1] += times * b; damage[pos + 1] += times * b; damage[pos] += times * a; build_path(pos + 1); } int main() { memset(mem, -1, sizeof(mem)); std::cin >> n >> a >> b; for (int i = 1; i <= n; ++i) std::cin >> health[i]; std::cout << solve(2) << std::endl; build_path(2); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x * f; } int n, a, b, h[15], tot, ans; int p[155], val[155]; inline void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; i++) p[++tot] = n - 1; if (ans > tot) { for (int i = 1; i <= tot; i++) val[i] = p[i]; ans = tot; } tot -= yu; } inline void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int last = tot, A = h[now - 1], B = h[now], C = h[now + 1]; while (1) { h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; p[++tot] = now; if (h[now - 1] < 0) dfs(now + 1); if ((h[now - 1] < 0 && h[now] < 0) || tot >= ans) { tot = last; h[now - 1] = A; h[now] = B; h[now + 1] = C; break; } } } signed main() { n = read(); a = read(); b = read(); for (int i = 1; i <= n; i++) h[i] = read(); ans = 2147483647; tot = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i <= ans; i++) printf("%d ", val[i]); return 0; }
### Prompt Develop a solution in CPP to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x * f; } int n, a, b, h[15], tot, ans; int p[155], val[155]; inline void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; i++) p[++tot] = n - 1; if (ans > tot) { for (int i = 1; i <= tot; i++) val[i] = p[i]; ans = tot; } tot -= yu; } inline void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int last = tot, A = h[now - 1], B = h[now], C = h[now + 1]; while (1) { h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; p[++tot] = now; if (h[now - 1] < 0) dfs(now + 1); if ((h[now - 1] < 0 && h[now] < 0) || tot >= ans) { tot = last; h[now - 1] = A; h[now] = B; h[now + 1] = C; break; } } } signed main() { n = read(); a = read(); b = read(); for (int i = 1; i <= n; i++) h[i] = read(); ans = 2147483647; tot = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i <= ans; i++) printf("%d ", val[i]); return 0; } ```
#include <bits/stdc++.h> int n, a, b, h[10]; int dp[10][17][17][17]; int p[10][17][17][17][3]; int c[10], r[170], rn; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < (int)(n); i++) scanf("%d", h + i); for (int i = 0; i < (int)(10); i++) for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) for (int z = 0; z < (int)(17); z++) dp[i][x][y][z] = (1000000); dp[0][0][0][0] = 0; for (int i = 1; i < n; i++) { for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) for (int z = 0; z < (int)(17); z++) for (int t = 0; t < (int)((i < n - 1) ? 17 : 1); t++) { if ((y + t) * b + z * a <= h[i - 1]) continue; int nc = dp[i - 1][x][y][z] + t; if (dp[i][y][z][t] > nc) { dp[i][y][z][t] = nc; p[i][y][z][t][0] = x; p[i][y][z][t][1] = y; p[i][y][z][t][2] = z; } } } int ans = (1000000); int tx, ty, tz; for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) if (y * b > h[n - 1]) { if (ans > dp[n - 1][x][y][0]) { ans = dp[n - 1][x][y][0]; tx = x; ty = y; tz = 0; } } printf("%d\n", ans); int cur = n - 1; while (cur >= 0) { c[cur] = tz; int px, py, pz; px = p[cur][tx][ty][tz][0]; py = p[cur][tx][ty][tz][1]; pz = p[cur][tx][ty][tz][2]; tx = px; ty = py; tz = pz; cur = cur - 1; } rn = 0; for (int i = 1; i < n - 1; i++) { for (int j = 0; j < (int)(c[i]); j++) r[rn++] = i + 1; } for (int i = 0; i < (int)(rn); i++) printf("%d%c", r[i], i == rn - 1 ? '\n' : ' '); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> int n, a, b, h[10]; int dp[10][17][17][17]; int p[10][17][17][17][3]; int c[10], r[170], rn; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < (int)(n); i++) scanf("%d", h + i); for (int i = 0; i < (int)(10); i++) for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) for (int z = 0; z < (int)(17); z++) dp[i][x][y][z] = (1000000); dp[0][0][0][0] = 0; for (int i = 1; i < n; i++) { for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) for (int z = 0; z < (int)(17); z++) for (int t = 0; t < (int)((i < n - 1) ? 17 : 1); t++) { if ((y + t) * b + z * a <= h[i - 1]) continue; int nc = dp[i - 1][x][y][z] + t; if (dp[i][y][z][t] > nc) { dp[i][y][z][t] = nc; p[i][y][z][t][0] = x; p[i][y][z][t][1] = y; p[i][y][z][t][2] = z; } } } int ans = (1000000); int tx, ty, tz; for (int x = 0; x < (int)(17); x++) for (int y = 0; y < (int)(17); y++) if (y * b > h[n - 1]) { if (ans > dp[n - 1][x][y][0]) { ans = dp[n - 1][x][y][0]; tx = x; ty = y; tz = 0; } } printf("%d\n", ans); int cur = n - 1; while (cur >= 0) { c[cur] = tz; int px, py, pz; px = p[cur][tx][ty][tz][0]; py = p[cur][tx][ty][tz][1]; pz = p[cur][tx][ty][tz][2]; tx = px; ty = py; tz = pz; cur = cur - 1; } rn = 0; for (int i = 1; i < n - 1; i++) { for (int j = 0; j < (int)(c[i]); j++) r[rn++] = i + 1; } for (int i = 0; i < (int)(rn); i++) printf("%d%c", r[i], i == rn - 1 ? '\n' : ' '); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAX = 15 + 5; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 2000; int n, a, b, x[MAX], dp[MAX][MAX][MAX]; vector<int> ans; int f(int nw, int pvv, int pv) { if (pvv < 0) pvv = 0; if (pv < 0) pv = 0; if (nw == n + 1) return (pvv ? 1e9 : 0); if (dp[nw][pvv][pv] != -1) return dp[nw][pvv][pv]; int &ret = dp[nw][pvv][pv]; ret = 1e9; for (int i = 0; i <= 15; ++i) { if (pvv <= 0) ret = min(ret, f(nw + 1, pv, x[nw] - b * i) + i); pv -= a, pvv -= b; } return ret; } void bt(int nw, int pvv, int pv) { if (pvv < 0) pvv = 0; if (pv < 0) pv = 0; if (nw == n + 1) return; int ret = dp[nw][pvv][pv]; for (int i = 0; i <= 15; ++i) { if (pvv <= 0) { if (ret == f(nw + 1, pv, x[nw] - b * i) + i) { bt(nw + 1, pv, x[nw] - b * i); break; } } ans.push_back(nw - 1); pv -= a, pvv -= b; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> a >> b; for (int i = 1; i <= n; ++i) cin >> x[i], ++x[i]; while (x[1] > 0) ans.push_back(2), x[1] -= b, x[2] -= a, x[3] -= b; while (x[n] > 0) ans.push_back(n - 1), x[n - 2] -= b, x[n - 1] -= a, x[n] -= b; memset(dp, -1, sizeof dp); f(3, x[1], x[2]); bt(3, x[1], x[2]); cout << ans.size() << '\n'; for (auto i : ans) cout << i << ' '; return 0; }
### Prompt Develop a solution in cpp to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAX = 15 + 5; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 2000; int n, a, b, x[MAX], dp[MAX][MAX][MAX]; vector<int> ans; int f(int nw, int pvv, int pv) { if (pvv < 0) pvv = 0; if (pv < 0) pv = 0; if (nw == n + 1) return (pvv ? 1e9 : 0); if (dp[nw][pvv][pv] != -1) return dp[nw][pvv][pv]; int &ret = dp[nw][pvv][pv]; ret = 1e9; for (int i = 0; i <= 15; ++i) { if (pvv <= 0) ret = min(ret, f(nw + 1, pv, x[nw] - b * i) + i); pv -= a, pvv -= b; } return ret; } void bt(int nw, int pvv, int pv) { if (pvv < 0) pvv = 0; if (pv < 0) pv = 0; if (nw == n + 1) return; int ret = dp[nw][pvv][pv]; for (int i = 0; i <= 15; ++i) { if (pvv <= 0) { if (ret == f(nw + 1, pv, x[nw] - b * i) + i) { bt(nw + 1, pv, x[nw] - b * i); break; } } ans.push_back(nw - 1); pv -= a, pvv -= b; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> a >> b; for (int i = 1; i <= n; ++i) cin >> x[i], ++x[i]; while (x[1] > 0) ans.push_back(2), x[1] -= b, x[2] -= a, x[3] -= b; while (x[n] > 0) ans.push_back(n - 1), x[n - 2] -= b, x[n - 1] -= a, x[n] -= b; memset(dp, -1, sizeof dp); f(3, x[1], x[2]); bt(3, x[1], x[2]); cout << ans.size() << '\n'; for (auto i : ans) cout << i << ' '; return 0; } ```
#include <bits/stdc++.h> int n, a, b; std::array<int, 100> h{0}; std::vector<int> Vec, Temp; int Ans = INT_MAX; int Read() { int n = 0, k = 1; char ch = getchar(); while ((ch > '9' || ch < '0') && ch != '-') ch = getchar(); if (ch == '-') { k = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') { n = n * 10 + ch - '0'; ch = getchar(); } return n * k; } void DFS(int Now, int Sum) { if (Sum >= Ans) { return; } if (Now == n - 1) { if (h[n - 1] < 0) { Ans = Sum; Vec = Temp; } return; } for (int i = 0; i <= std::max({(h[Now - 1] / b), (h[Now] / a), (h[Now + 1] / b)}) + 1; ++i) { if (h[Now - 1] - b * i < 0) { h[Now - 1] -= b * i; h[Now] -= a * i; h[Now + 1] -= b * i; for (int t = 0; t < i; ++t) { Temp.push_back(Now + 1); } DFS(Now + 1, Sum + i); h[Now - 1] += b * i; h[Now] += a * i; h[Now + 1] += b * i; for (int t = 0; t < i; ++t) { Temp.pop_back(); } } } return; } int main() { n = Read(); a = Read(); b = Read(); for (int i = 0; i < n; ++i) { h[i] = Read(); } DFS(1, 0); std::cout << Ans << std::endl; for (const auto &r : Vec) { std::cout << r << ' '; } std::putchar('\n'); return 0; }
### Prompt Create a solution in cpp for the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> int n, a, b; std::array<int, 100> h{0}; std::vector<int> Vec, Temp; int Ans = INT_MAX; int Read() { int n = 0, k = 1; char ch = getchar(); while ((ch > '9' || ch < '0') && ch != '-') ch = getchar(); if (ch == '-') { k = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') { n = n * 10 + ch - '0'; ch = getchar(); } return n * k; } void DFS(int Now, int Sum) { if (Sum >= Ans) { return; } if (Now == n - 1) { if (h[n - 1] < 0) { Ans = Sum; Vec = Temp; } return; } for (int i = 0; i <= std::max({(h[Now - 1] / b), (h[Now] / a), (h[Now + 1] / b)}) + 1; ++i) { if (h[Now - 1] - b * i < 0) { h[Now - 1] -= b * i; h[Now] -= a * i; h[Now + 1] -= b * i; for (int t = 0; t < i; ++t) { Temp.push_back(Now + 1); } DFS(Now + 1, Sum + i); h[Now - 1] += b * i; h[Now] += a * i; h[Now + 1] += b * i; for (int t = 0; t < i; ++t) { Temp.pop_back(); } } } return; } int main() { n = Read(); a = Read(); b = Read(); for (int i = 0; i < n; ++i) { h[i] = Read(); } DFS(1, 0); std::cout << Ans << std::endl; for (const auto &r : Vec) { std::cout << r << ' '; } std::putchar('\n'); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a, b; vector<int> h; int ans = 1e8; int dp[100][100][100]; int path[100][100][100]; int solve(int p, int l, int c) { int ans = 1e8; if (p == n - 1) { if (l < 0 && c < 0) return 0; else return int(1e8); } if (dp[p + 80][l + 80][c + 80] != -1) return dp[p + 80][l + 80][c + 80]; for (int i = 0; i <= max(l / b + 1, max(h[p + 1] / b + 1, c / a + 1)); i++) { if (l - i * b < 0) { int tmp = ans; ans = min(ans, i + solve(p + 1, c - i * a, h[p + 1] - b * i)); if (ans != tmp) path[p][l][c] = i; } } return dp[p + 80][l + 80][c + 80] = ans; } void print_path(int p, int l, int c) { if (p == n - 1) return; for (int i = 0; i < path[p][l][c]; i++) cout << p + 1 << " "; int c1 = h[p + 1] - path[p][l][c] * b; int l1 = c - path[p][l][c] * a; print_path(p + 1, l1, c1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(dp, -1, sizeof dp); cin >> n >> a >> b; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; h.push_back(tmp); } cout << solve(1, h[0], h[1]) << endl; print_path(1, h[0], h[1]); }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b; vector<int> h; int ans = 1e8; int dp[100][100][100]; int path[100][100][100]; int solve(int p, int l, int c) { int ans = 1e8; if (p == n - 1) { if (l < 0 && c < 0) return 0; else return int(1e8); } if (dp[p + 80][l + 80][c + 80] != -1) return dp[p + 80][l + 80][c + 80]; for (int i = 0; i <= max(l / b + 1, max(h[p + 1] / b + 1, c / a + 1)); i++) { if (l - i * b < 0) { int tmp = ans; ans = min(ans, i + solve(p + 1, c - i * a, h[p + 1] - b * i)); if (ans != tmp) path[p][l][c] = i; } } return dp[p + 80][l + 80][c + 80] = ans; } void print_path(int p, int l, int c) { if (p == n - 1) return; for (int i = 0; i < path[p][l][c]; i++) cout << p + 1 << " "; int c1 = h[p + 1] - path[p][l][c] * b; int l1 = c - path[p][l][c] * a; print_path(p + 1, l1, c1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(dp, -1, sizeof dp); cin >> n >> a >> b; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; h.push_back(tmp); } cout << solve(1, h[0], h[1]) << endl; print_path(1, h[0], h[1]); } ```
#include <bits/stdc++.h> using namespace std; int n, a, b, h[20]; int dp[15][20][20]; vector<int> path[15][20][20]; int main() { while (cin >> n >> a >> b) { for (int i = 0; i < n; i++) { cin >> h[i]; h[i]++; } int r1 = (h[0] + b - 1) / b; h[1] = max(0, h[1] - r1 * a); h[2] = max(0, h[2] - r1 * b); int r2 = (h[n - 1] + b - 1) / b; h[n - 2] = max(0, h[n - 2] - r1 * a); h[n - 3] = max(h[n - 3] - r2 * b, 0); memset(dp, 0x11, sizeof(dp)); dp[1][0][h[1]] = 0; for (int i = 1; i < n - 1; i++) { for (int j = 0; j <= 16; j++) { for (int k = 0; k <= 16; k++) { if (dp[i][j][k] == 0x11111111) continue; for (int r = (j + b - 1) / b; r <= 16; r++) { int nextj = max(0, k - r * a); int nextk = max(0, h[i + 1] - r * b); if (dp[i + 1][nextj][nextk] > dp[i][j][k] + r) { dp[i + 1][nextj][nextk] = dp[i][j][k] + r; path[i + 1][nextj][nextk] = path[i][j][k]; for (int rr = 1; rr <= r; rr++) path[i + 1][nextj][nextk].push_back(i + 1); } } } } } int ans = 0x11111111, i1, i2; for (int i = 0; i <= 16; i++) for (int j = 0; j <= 16; j++) if (dp[n - 1][i][j] < ans) ans = dp[n - 1][i][j], i1 = i, i2 = j; for (int i = 0; i < r1; i++) path[n - 1][i1][i2].push_back(2); for (int i = 0; i < r2; i++) path[n - 1][i1][i2].push_back(n - 1); printf("%d\n", path[n - 1][i1][i2].size()); for (int i = 0; i < path[n - 1][i1][i2].size(); i++) printf("%d%c", path[n - 1][i1][i2][i], i < path[n - 1][i1][i2].size() - 1 ? ' ' : '\n'); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b, h[20]; int dp[15][20][20]; vector<int> path[15][20][20]; int main() { while (cin >> n >> a >> b) { for (int i = 0; i < n; i++) { cin >> h[i]; h[i]++; } int r1 = (h[0] + b - 1) / b; h[1] = max(0, h[1] - r1 * a); h[2] = max(0, h[2] - r1 * b); int r2 = (h[n - 1] + b - 1) / b; h[n - 2] = max(0, h[n - 2] - r1 * a); h[n - 3] = max(h[n - 3] - r2 * b, 0); memset(dp, 0x11, sizeof(dp)); dp[1][0][h[1]] = 0; for (int i = 1; i < n - 1; i++) { for (int j = 0; j <= 16; j++) { for (int k = 0; k <= 16; k++) { if (dp[i][j][k] == 0x11111111) continue; for (int r = (j + b - 1) / b; r <= 16; r++) { int nextj = max(0, k - r * a); int nextk = max(0, h[i + 1] - r * b); if (dp[i + 1][nextj][nextk] > dp[i][j][k] + r) { dp[i + 1][nextj][nextk] = dp[i][j][k] + r; path[i + 1][nextj][nextk] = path[i][j][k]; for (int rr = 1; rr <= r; rr++) path[i + 1][nextj][nextk].push_back(i + 1); } } } } } int ans = 0x11111111, i1, i2; for (int i = 0; i <= 16; i++) for (int j = 0; j <= 16; j++) if (dp[n - 1][i][j] < ans) ans = dp[n - 1][i][j], i1 = i, i2 = j; for (int i = 0; i < r1; i++) path[n - 1][i1][i2].push_back(2); for (int i = 0; i < r2; i++) path[n - 1][i1][i2].push_back(n - 1); printf("%d\n", path[n - 1][i1][i2].size()); for (int i = 0; i < path[n - 1][i1][i2].size(); i++) printf("%d%c", path[n - 1][i1][i2][i], i < path[n - 1][i1][i2].size() - 1 ? ' ' : '\n'); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int h[22], dp[12][20][20]; pair<int, int> pre[12][20][20]; void fmain(int tid) { int n, a, b; cin >> n >> a >> b; for (int(i) = 1; (i) <= (int)(n); (i)++) cin >> h[i]; for (int(i) = 1; (i) <= (int)(n); (i)++) h[i]++; for (int(i) = 0; (i) < (int)(12); (i)++) for (int(j) = 0; (j) < (int)(20); (j)++) for (int(k) = 0; (k) < (int)(20); (k)++) { dp[i][j][k] = (1 << 30); } dp[1][h[1]][0] = 0; for (int i = 2; i < n; i++) { for (int j = 0; j <= h[i - 1]; j++) { for (int(k) = 0; (k) < (int)(20); (k)++) if (dp[i - 1][j][k] < (1 << 30)) { for (int(x) = 0; (x) < (int)(20); (x)++) { if (j - b * x > 0) continue; int nj = max(h[i] - b * k - a * x, 0); if (dp[i][nj][x] > dp[i - 1][j][k] + x) { dp[i][nj][x] = dp[i - 1][j][k] + x; pre[i][nj][x] = {j, k}; } } } } } int ans = (1 << 30); for (int(x) = 0; (x) < (int)(20); (x)++) if (b * x >= h[n]) { mintt(ans, dp[n - 1][0][x]); } printf("%d\n", ans); for (int(x) = 0; (x) < (int)(20); (x)++) if (b * x >= h[n]) { if (ans == dp[n - 1][0][x]) { for (int pos = n - 1, cnt = x, H = 0; pos > 1; pos--) { for (int(tt) = 0; (tt) < (int)(cnt); (tt)++) printf("%d ", pos); auto pr = pre[pos][H][cnt]; cnt = pr.second; H = pr.first; } return; } } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int h[22], dp[12][20][20]; pair<int, int> pre[12][20][20]; void fmain(int tid) { int n, a, b; cin >> n >> a >> b; for (int(i) = 1; (i) <= (int)(n); (i)++) cin >> h[i]; for (int(i) = 1; (i) <= (int)(n); (i)++) h[i]++; for (int(i) = 0; (i) < (int)(12); (i)++) for (int(j) = 0; (j) < (int)(20); (j)++) for (int(k) = 0; (k) < (int)(20); (k)++) { dp[i][j][k] = (1 << 30); } dp[1][h[1]][0] = 0; for (int i = 2; i < n; i++) { for (int j = 0; j <= h[i - 1]; j++) { for (int(k) = 0; (k) < (int)(20); (k)++) if (dp[i - 1][j][k] < (1 << 30)) { for (int(x) = 0; (x) < (int)(20); (x)++) { if (j - b * x > 0) continue; int nj = max(h[i] - b * k - a * x, 0); if (dp[i][nj][x] > dp[i - 1][j][k] + x) { dp[i][nj][x] = dp[i - 1][j][k] + x; pre[i][nj][x] = {j, k}; } } } } } int ans = (1 << 30); for (int(x) = 0; (x) < (int)(20); (x)++) if (b * x >= h[n]) { mintt(ans, dp[n - 1][0][x]); } printf("%d\n", ans); for (int(x) = 0; (x) < (int)(20); (x)++) if (b * x >= h[n]) { if (ans == dp[n - 1][0][x]) { for (int pos = n - 1, cnt = x, H = 0; pos > 1; pos--) { for (int(tt) = 0; (tt) < (int)(cnt); (tt)++) printf("%d ", pos); auto pr = pre[pos][H][cnt]; cnt = pr.second; H = pr.first; } return; } } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 20; int h[N]; int ans = 1 << 30; vector<int> T, T2; int n, a, b; void dfs(int cur, int times) { if (times >= ans) return; if (cur == n) { if (h[cur] < 0) { T2 = T; ans = times; } return; } for (int i = 0; i <= max(h[cur - 1] / b + 1, max(h[cur] / a + 1, h[cur + 1] / b + 1)); ++i) { if (h[cur - 1] < b * i) { h[cur - 1] -= b * i, h[cur] -= a * i, h[cur + 1] -= b * i; for (int j = 0; j < i; ++j) T.push_back(cur); dfs(cur + 1, times + i); for (int j = 0; j < i; ++j) T.pop_back(); h[cur - 1] += b * i, h[cur] += a * i, h[cur + 1] += b * i; } } } int main() { cin >> n >> a >> b; for (int i = 1; i <= n; ++i) cin >> h[i]; dfs(2, 0); cout << ans << endl; for (auto &x : T2) cout << x << " "; cout << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 20; int h[N]; int ans = 1 << 30; vector<int> T, T2; int n, a, b; void dfs(int cur, int times) { if (times >= ans) return; if (cur == n) { if (h[cur] < 0) { T2 = T; ans = times; } return; } for (int i = 0; i <= max(h[cur - 1] / b + 1, max(h[cur] / a + 1, h[cur + 1] / b + 1)); ++i) { if (h[cur - 1] < b * i) { h[cur - 1] -= b * i, h[cur] -= a * i, h[cur + 1] -= b * i; for (int j = 0; j < i; ++j) T.push_back(cur); dfs(cur + 1, times + i); for (int j = 0; j < i; ++j) T.pop_back(); h[cur - 1] += b * i, h[cur] += a * i, h[cur + 1] += b * i; } } } int main() { cin >> n >> a >> b; for (int i = 1; i <= n; ++i) cin >> h[i]; dfs(2, 0); cout << ans << endl; for (auto &x : T2) cout << x << " "; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 20; int h[maxn], f[100], n, ans; int a, b; bool dfs(int d, int x, int l) { if (d <= 0) { for (int i = 1; i <= n; i++) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(d, x + 1, l); if (x == n) { h[n] -= b; f[d] = n - 1; if (dfs(d - 1, x, l)) return true; h[n] += b; } else { for (int i = max(x, l); i <= x + 1; i++) { if (i > 1 && i < n) { h[i] -= a; h[i - 1] -= b; h[i + 1] -= b; f[d] = i; if (dfs(d - 1, x, i)) return true; h[i] += a; h[i - 1] += b; h[i + 1] += b; } } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (ans = 1;; ans++) { if (dfs(ans, 1, 2)) { cout << ans << endl; for (int i = 1; i <= ans; i++) cout << f[i] << ' '; cout << endl; return 0; } } return 0; }
### Prompt Develop a solution in CPP to the problem described below: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 20; int h[maxn], f[100], n, ans; int a, b; bool dfs(int d, int x, int l) { if (d <= 0) { for (int i = 1; i <= n; i++) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(d, x + 1, l); if (x == n) { h[n] -= b; f[d] = n - 1; if (dfs(d - 1, x, l)) return true; h[n] += b; } else { for (int i = max(x, l); i <= x + 1; i++) { if (i > 1 && i < n) { h[i] -= a; h[i - 1] -= b; h[i + 1] -= b; f[d] = i; if (dfs(d - 1, x, i)) return true; h[i] += a; h[i - 1] += b; h[i + 1] += b; } } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (ans = 1;; ans++) { if (dfs(ans, 1, 2)) { cout << ans << endl; for (int i = 1; i <= ans; i++) cout << f[i] << ' '; cout << endl; return 0; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int oo = 999999999; const double PI = 3.1415931; const double eps = 1e-9; const int maxN = 10 + 10; int health[maxN]; int n, a, b; int ans = oo; vector<int> v; int used[maxN]; void go(int pos, int tot) { if (tot >= ans) return; if (pos == n) { if (health[pos] < 0 && ans > tot) { ans = min(ans, tot); v.clear(); for (int i = 1; i <= n; i++) { v.push_back(used[i]); } } return; } int bound = max(max(health[pos - 1] / b + 1, health[pos] / a + 1), health[pos + 1] / b + 1); for (int i = 0; i <= bound; i++) { health[pos - 1] -= i * b; health[pos] -= i * a; health[pos + 1] -= i * b; used[pos] += i; if (health[pos - 1] < 0) { go(pos + 1, tot + i); } used[pos] -= i; health[pos - 1] += i * b; health[pos] += i * a; health[pos + 1] += i * b; } return; } int main() { ios_base::sync_with_stdio(false); cin >> n >> a >> b; for (int i = 1; i <= n; i++) { cin >> health[i]; } go(2, 0); cout << ans << endl; for (int i = 0; i < v.size(); i++) { for (int j = 1; j <= v[i]; j++) cout << i + 1 << " "; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int oo = 999999999; const double PI = 3.1415931; const double eps = 1e-9; const int maxN = 10 + 10; int health[maxN]; int n, a, b; int ans = oo; vector<int> v; int used[maxN]; void go(int pos, int tot) { if (tot >= ans) return; if (pos == n) { if (health[pos] < 0 && ans > tot) { ans = min(ans, tot); v.clear(); for (int i = 1; i <= n; i++) { v.push_back(used[i]); } } return; } int bound = max(max(health[pos - 1] / b + 1, health[pos] / a + 1), health[pos + 1] / b + 1); for (int i = 0; i <= bound; i++) { health[pos - 1] -= i * b; health[pos] -= i * a; health[pos + 1] -= i * b; used[pos] += i; if (health[pos - 1] < 0) { go(pos + 1, tot + i); } used[pos] -= i; health[pos - 1] += i * b; health[pos] += i * a; health[pos + 1] += i * b; } return; } int main() { ios_base::sync_with_stdio(false); cin >> n >> a >> b; for (int i = 1; i <= n; i++) { cin >> health[i]; } go(2, 0); cout << ans << endl; for (int i = 0; i < v.size(); i++) { for (int j = 1; j <= v[i]; j++) cout << i + 1 << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, h[22], click[22], a, b, ans, gas[22]; vector<int> v; bool ok() { for (int i = 2; i < n; i++) { if (h[i] >= click[i - 1] * b + click[i] * a + click[i + 1] * b) return 0; } return 1; } int main() { scanf("%d %d %d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); if (n == 3) { ans = max(h[1] / b, max(h[2] / a, h[3] / b)) + 1; printf("%d\n", ans); for (int i = 1; i < ans; i++) printf("2 "); puts("2"); } else { int x = h[1] / b + 1, y = h[n] / b + 1; gas[2] = x; gas[n - 1] = y; h[2] -= x * a; h[n - 1] -= y * a; h[3] -= x * b; h[n - 2] -= y * b; int M = 1; for (int i = 0; i < n - 2; i++) M *= 9; int mn = M, Ans = 0; for (int i = 0; i < M; i++) { int I = i, rlt = 0; for (int j = 2; j <= n - 1; j++) click[j] = I % 9, I /= 9, rlt += click[j]; if (rlt < mn && ok()) mn = rlt, Ans = i; } for (int j = 2; j <= n - 1; j++) gas[j] += Ans % 9, Ans /= 9; printf("%d\n", mn + x + y); for (int j = 2; j <= n - 1; j++) { for (int k = 0; k < gas[j]; k++) v.push_back(j); } for (int i = 0; i < v.size() - 1; i++) printf("%d ", v[i]); printf("%d\n", v[v.size() - 1]); } return 0; }
### Prompt Please create a solution in CPP to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, h[22], click[22], a, b, ans, gas[22]; vector<int> v; bool ok() { for (int i = 2; i < n; i++) { if (h[i] >= click[i - 1] * b + click[i] * a + click[i + 1] * b) return 0; } return 1; } int main() { scanf("%d %d %d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); if (n == 3) { ans = max(h[1] / b, max(h[2] / a, h[3] / b)) + 1; printf("%d\n", ans); for (int i = 1; i < ans; i++) printf("2 "); puts("2"); } else { int x = h[1] / b + 1, y = h[n] / b + 1; gas[2] = x; gas[n - 1] = y; h[2] -= x * a; h[n - 1] -= y * a; h[3] -= x * b; h[n - 2] -= y * b; int M = 1; for (int i = 0; i < n - 2; i++) M *= 9; int mn = M, Ans = 0; for (int i = 0; i < M; i++) { int I = i, rlt = 0; for (int j = 2; j <= n - 1; j++) click[j] = I % 9, I /= 9, rlt += click[j]; if (rlt < mn && ok()) mn = rlt, Ans = i; } for (int j = 2; j <= n - 1; j++) gas[j] += Ans % 9, Ans /= 9; printf("%d\n", mn + x + y); for (int j = 2; j <= n - 1; j++) { for (int k = 0; k < gas[j]; k++) v.push_back(j); } for (int i = 0; i < v.size() - 1; i++) printf("%d ", v[i]); printf("%d\n", v[v.size() - 1]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Node { int len, state; Node *next; Node(int len = -1, Node *next = NULL, int state = 0) : len(len), state(state), next(next) { ; } }; Node lastG(0), lastB(-1); Node memo[11][20][20]; int hp[11], n, a, b, bestLen = -1; void shoot(int pos, int count = 1) { hp[pos] = max(hp[pos] - a * count, 0); hp[pos + 1] = max(hp[pos + 1] - b * count, 0); hp[pos - 1] = max(hp[pos - 1] - b * count, 0); } Node *solve(int pos, int len) { if (pos == n - 1) { if (hp[pos - 1] == 0 && hp[pos] == 0) { bestLen = bestLen == -1 ? len : min(bestLen, len); return &lastG; } return &lastB; } if (len >= bestLen && bestLen != -1) { return &lastB; } Node *best = &memo[pos][hp[pos]][hp[pos - 1]], *cur; if (best->next) { bestLen = bestLen == -1 ? len : min(bestLen, len + best->len); return best; } int t1, t2, t3, state = 0; t1 = hp[pos - 1]; t2 = hp[pos]; t3 = hp[pos + 1]; while (true) { if (hp[pos - 1] == 0) { cur = solve(pos + 1, len + state); if (cur->len != -1 && (cur->len + state < best->len || best->len == -1)) { best->len = cur->len + state; best->next = cur; best->state = state; } if (hp[pos] == 0 && hp[pos + 1] == 0 && hp[pos - 1] == 0) { break; } } shoot(pos); ++state; } hp[pos - 1] = t1; hp[pos] = t2; hp[pos + 1] = t3; return best; } int main(int argc, char **argv) { cin >> n >> a >> b; for (int i = 0; i < n; ++i) { cin >> hp[i]; hp[i] += 1; } Node *rez = solve(1, 0); cout << rez->len << endl; int pos = 2; while (rez != &lastG) { for (int i = 0; i < rez->state; ++i) { if (pos != 2 || i != 0) { cout << ' '; } cout << pos; } rez = rez->next; ++pos; } cout << endl; }
### Prompt Your task is to create a cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Node { int len, state; Node *next; Node(int len = -1, Node *next = NULL, int state = 0) : len(len), state(state), next(next) { ; } }; Node lastG(0), lastB(-1); Node memo[11][20][20]; int hp[11], n, a, b, bestLen = -1; void shoot(int pos, int count = 1) { hp[pos] = max(hp[pos] - a * count, 0); hp[pos + 1] = max(hp[pos + 1] - b * count, 0); hp[pos - 1] = max(hp[pos - 1] - b * count, 0); } Node *solve(int pos, int len) { if (pos == n - 1) { if (hp[pos - 1] == 0 && hp[pos] == 0) { bestLen = bestLen == -1 ? len : min(bestLen, len); return &lastG; } return &lastB; } if (len >= bestLen && bestLen != -1) { return &lastB; } Node *best = &memo[pos][hp[pos]][hp[pos - 1]], *cur; if (best->next) { bestLen = bestLen == -1 ? len : min(bestLen, len + best->len); return best; } int t1, t2, t3, state = 0; t1 = hp[pos - 1]; t2 = hp[pos]; t3 = hp[pos + 1]; while (true) { if (hp[pos - 1] == 0) { cur = solve(pos + 1, len + state); if (cur->len != -1 && (cur->len + state < best->len || best->len == -1)) { best->len = cur->len + state; best->next = cur; best->state = state; } if (hp[pos] == 0 && hp[pos + 1] == 0 && hp[pos - 1] == 0) { break; } } shoot(pos); ++state; } hp[pos - 1] = t1; hp[pos] = t2; hp[pos + 1] = t3; return best; } int main(int argc, char **argv) { cin >> n >> a >> b; for (int i = 0; i < n; ++i) { cin >> hp[i]; hp[i] += 1; } Node *rez = solve(1, 0); cout << rez->len << endl; int pos = 2; while (rez != &lastG) { for (int i = 0; i < rez->state; ++i) { if (pos != 2 || i != 0) { cout << ' '; } cout << pos; } rez = rez->next; ++pos; } cout << endl; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e1 + 5, maxm = 15 * 10 + 5; int n, a, b, h[maxn], sum, ans; int pos[maxm], val[maxm]; int rad() { int ret = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return ret * f; } void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; ++i) pos[++sum] = n - 1; if (ans > sum) { for (int i = 1; i <= sum; ++i) val[i] = pos[i]; ans = sum; } sum -= yu; } void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int i = 1, lst = sum, A = h[now - 1], B = h[now], C = h[now + 1]; while (true) { h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; pos[++sum] = now; if (h[now - 1] < 0) dfs(now + 1); if ((h[now - 1] < 0 && h[now] < 0) || sum >= ans) { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } } } int main() { n = rad(); a = rad(); b = rad(); for (int i = 1; i <= n; ++i) h[i] = rad(); ans = 1e9; sum = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i < ans; ++i) printf("%d ", val[i]); printf("%d\n", val[ans]); return 0; }
### Prompt In CPP, your task is to solve the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e1 + 5, maxm = 15 * 10 + 5; int n, a, b, h[maxn], sum, ans; int pos[maxm], val[maxm]; int rad() { int ret = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return ret * f; } void check() { int yu = max(h[n - 1] / a + (h[n - 1] >= 0), h[n] / b + (h[n] >= 0)); for (int i = 1; i <= yu; ++i) pos[++sum] = n - 1; if (ans > sum) { for (int i = 1; i <= sum; ++i) val[i] = pos[i]; ans = sum; } sum -= yu; } void dfs(int now) { if (now > n - 1) { check(); return; } if (h[now - 1] < 0) dfs(now + 1); int i = 1, lst = sum, A = h[now - 1], B = h[now], C = h[now + 1]; while (true) { h[now] -= a; h[now - 1] -= b; h[now + 1] -= b; pos[++sum] = now; if (h[now - 1] < 0) dfs(now + 1); if ((h[now - 1] < 0 && h[now] < 0) || sum >= ans) { sum = lst; h[now - 1] = A, h[now] = B, h[now + 1] = C; break; } } } int main() { n = rad(); a = rad(); b = rad(); for (int i = 1; i <= n; ++i) h[i] = rad(); ans = 1e9; sum = 0; dfs(2); printf("%d\n", ans); for (int i = 1; i < ans; ++i) printf("%d ", val[i]); printf("%d\n", val[ans]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a, b, h[20], rec[100]; bool dfs(int tot, int x, int pos) { if (tot == 0) { for (int i = 1; i <= n; ++i) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(tot, x + 1, pos); if (x == n) { h[n] -= b, rec[tot] = n - 1; if (dfs(tot - 1, x, pos)) return true; h[n] += b; } else { for (int i = max(x, pos); i <= x + 1; ++i) if (i > 1 && i < n) { rec[tot] = i; h[i] -= a, h[i - 1] -= b, h[i + 1] -= b; if (dfs(tot - 1, x, i)) return true; h[i] += a, h[i - 1] += b, h[i + 1] += b; } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); for (int ans = 1;; ++ans) { if (dfs(ans, 1, 2)) { printf("%d\n", ans); for (int i = 1; i <= ans; ++i) printf("%d ", rec[i]); return 0; } } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b, h[20], rec[100]; bool dfs(int tot, int x, int pos) { if (tot == 0) { for (int i = 1; i <= n; ++i) if (h[i] >= 0) return false; return true; } if (h[x] < 0) return dfs(tot, x + 1, pos); if (x == n) { h[n] -= b, rec[tot] = n - 1; if (dfs(tot - 1, x, pos)) return true; h[n] += b; } else { for (int i = max(x, pos); i <= x + 1; ++i) if (i > 1 && i < n) { rec[tot] = i; h[i] -= a, h[i - 1] -= b, h[i + 1] -= b; if (dfs(tot - 1, x, i)) return true; h[i] += a, h[i - 1] += b, h[i + 1] += b; } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); for (int ans = 1;; ++ans) { if (dfs(ans, 1, 2)) { printf("%d\n", ans); for (int i = 1; i <= ans; ++i) printf("%d ", rec[i]); return 0; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a, b, h[20], ans = 10000, an[20], no[20]; void dfs(int now, int res) { if (now == n) { if (res < ans) { ans = res; for (int i = 2; i < n; i++) an[i] = no[i]; } return; } if (now == n - 1) { int l = h[n] / b + 1, r = h[n - 1] / a + 1, t = h[n - 2] / b + 1; no[n - 1] = max(max(0, l), max(r, t)); dfs(now + 1, res + max(max(0, l), max(r, t))); } else if (now == 2) { int l = h[1] / b, r = h[2] / a, t = h[3] / b; if (h[1] >= 0) l++; if (h[2] >= 0) r++; if (h[3] >= 0) t++; l = max(0, l); r = max(l, max(r, t)); for (int i = l; i <= r; i++) { h[2] -= i * a; h[3] -= i * b; no[2] = i; dfs(now + 1, res + i); h[2] += i * a; h[3] += i * b; } } else { int l = h[now - 1] / b, r = h[now] / a, t = h[now + 1] / b; if (h[now - 1] >= 0) l++; if (h[now] >= 0) r++; if (h[now + 1] >= 0) t++; l = max(0, l); r = max(l, max(r, t)); for (int i = l; i <= r; i++) { h[now] -= i * a; h[now + 1] -= i * b; no[now] = i; dfs(now + 1, res + i); h[now] += i * a; h[now + 1] += i * b; } } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) for (int j = 1; j <= an[i]; j++) printf("%d ", i); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a, b, h[20], ans = 10000, an[20], no[20]; void dfs(int now, int res) { if (now == n) { if (res < ans) { ans = res; for (int i = 2; i < n; i++) an[i] = no[i]; } return; } if (now == n - 1) { int l = h[n] / b + 1, r = h[n - 1] / a + 1, t = h[n - 2] / b + 1; no[n - 1] = max(max(0, l), max(r, t)); dfs(now + 1, res + max(max(0, l), max(r, t))); } else if (now == 2) { int l = h[1] / b, r = h[2] / a, t = h[3] / b; if (h[1] >= 0) l++; if (h[2] >= 0) r++; if (h[3] >= 0) t++; l = max(0, l); r = max(l, max(r, t)); for (int i = l; i <= r; i++) { h[2] -= i * a; h[3] -= i * b; no[2] = i; dfs(now + 1, res + i); h[2] += i * a; h[3] += i * b; } } else { int l = h[now - 1] / b, r = h[now] / a, t = h[now + 1] / b; if (h[now - 1] >= 0) l++; if (h[now] >= 0) r++; if (h[now + 1] >= 0) t++; l = max(0, l); r = max(l, max(r, t)); for (int i = l; i <= r; i++) { h[now] -= i * a; h[now + 1] -= i * b; no[now] = i; dfs(now + 1, res + i); h[now] += i * a; h[now + 1] += i * b; } } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); dfs(2, 0); printf("%d\n", ans); for (int i = 2; i < n; i++) for (int j = 1; j <= an[i]; j++) printf("%d ", i); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int a[20], cnt[20], b[20], p, q, n, ans, pre1, pre2; void dfs(int pos, int cnt[], int total) { int t = a[pos] - q * cnt[pos - 1]; if (pos == n - 1) { if (t >= 0) cnt[pos] = t / p + 1; if (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - cnt[pos] * q >= 0) cnt[pos] += (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - cnt[pos] * q) / q + 1; total += cnt[pos]; if (total < ans) { ans = total; for (int i = 1; i <= n; i++) b[i] = cnt[i]; } cnt[pos] = 0; return; } int ed = max(t / p + 1, max(a[pos - 1] / q + 1, a[pos + 1] / q + 1)); for (int i = 0; i <= ed; i++) { if (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - i * q < 0) { cnt[pos] = i; dfs(pos + 1, cnt, total + i); cnt[pos] = 0; } } } int main() { while (scanf("%d%d%d", &n, &p, &q) == 3) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); pre1 = pre2 = 0; if (a[1] >= 0) { pre1 = a[1] / q + 1; a[1] -= pre1 * q; a[2] -= pre1 * p; a[3] -= pre1 * q; } if (a[n] >= 0) { pre2 = a[n] / q + 1; a[n] -= pre2 * q; a[n - 1] -= pre2 * p; a[n - 2] -= pre2 * q; } ans = INF; memset(cnt, 0, sizeof(cnt)); dfs(2, cnt, pre1 + pre2); printf("%d\n", ans); b[2] += pre1; b[n - 1] += pre2; bool first = false; for (int i = 2; i <= n - 1; i++) { for (int j = 1; j <= b[i]; j++) { if (!first) { printf("%d", i); first = true; } else printf(" %d", i); } } printf("\n"); } return 0; }
### Prompt Generate a Cpp solution to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int a[20], cnt[20], b[20], p, q, n, ans, pre1, pre2; void dfs(int pos, int cnt[], int total) { int t = a[pos] - q * cnt[pos - 1]; if (pos == n - 1) { if (t >= 0) cnt[pos] = t / p + 1; if (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - cnt[pos] * q >= 0) cnt[pos] += (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - cnt[pos] * q) / q + 1; total += cnt[pos]; if (total < ans) { ans = total; for (int i = 1; i <= n; i++) b[i] = cnt[i]; } cnt[pos] = 0; return; } int ed = max(t / p + 1, max(a[pos - 1] / q + 1, a[pos + 1] / q + 1)); for (int i = 0; i <= ed; i++) { if (a[pos - 1] - cnt[pos - 1] * p - cnt[pos - 2] * q - i * q < 0) { cnt[pos] = i; dfs(pos + 1, cnt, total + i); cnt[pos] = 0; } } } int main() { while (scanf("%d%d%d", &n, &p, &q) == 3) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); pre1 = pre2 = 0; if (a[1] >= 0) { pre1 = a[1] / q + 1; a[1] -= pre1 * q; a[2] -= pre1 * p; a[3] -= pre1 * q; } if (a[n] >= 0) { pre2 = a[n] / q + 1; a[n] -= pre2 * q; a[n - 1] -= pre2 * p; a[n - 2] -= pre2 * q; } ans = INF; memset(cnt, 0, sizeof(cnt)); dfs(2, cnt, pre1 + pre2); printf("%d\n", ans); b[2] += pre1; b[n - 1] += pre2; bool first = false; for (int i = 2; i <= n - 1; i++) { for (int j = 1; j <= b[i]; j++) { if (!first) { printf("%d", i); first = true; } else printf(" %d", i); } } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct solution { vector<int> all; int moves; solution() { moves = 0; all.resize(20); } void add(int h, int p) { all[p] += h; moves += h; } bool operator>(solution x) { if (moves != x.moves) return moves > x.moves; return all > x.all; } }; int n, a, b; int t[22]; solution dp[22][22][22]; bool vis[22][22][22]; solution f(int x, int y, int pos) { if (x < 0) x = 0; if (y < 0) y = 0; if (pos == n - 2) { int hits = max((max(x, t[pos + 1]) + b - 1) / b, (y + a - 1) / a); solution temp; temp.add(hits, pos); return temp; } if (vis[x][y][pos]) return dp[x][y][pos]; vis[x][y][pos] = 1; solution ret; ret.moves = 1e9; for (int hits = (x + b - 1) / b; hits < 20; hits++) { solution temp = f(y - a * hits, t[pos + 1] - b * hits, pos + 1); temp.add(hits, pos); if (ret > temp) ret = temp; } return dp[x][y][pos] = ret; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < (n); i++) scanf("%d", &t[i]); for (int i = 0; i < (n); i++) t[i]++; solution res = f(t[0], t[1], 1); printf("%d\n", res.moves); for (int i = 0; i < (n); i++) for (int j = 0; j < (res.all[i]); j++) printf("%d ", i + 1); printf("\n"); }
### Prompt Construct a CPP code solution to the problem outlined: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct solution { vector<int> all; int moves; solution() { moves = 0; all.resize(20); } void add(int h, int p) { all[p] += h; moves += h; } bool operator>(solution x) { if (moves != x.moves) return moves > x.moves; return all > x.all; } }; int n, a, b; int t[22]; solution dp[22][22][22]; bool vis[22][22][22]; solution f(int x, int y, int pos) { if (x < 0) x = 0; if (y < 0) y = 0; if (pos == n - 2) { int hits = max((max(x, t[pos + 1]) + b - 1) / b, (y + a - 1) / a); solution temp; temp.add(hits, pos); return temp; } if (vis[x][y][pos]) return dp[x][y][pos]; vis[x][y][pos] = 1; solution ret; ret.moves = 1e9; for (int hits = (x + b - 1) / b; hits < 20; hits++) { solution temp = f(y - a * hits, t[pos + 1] - b * hits, pos + 1); temp.add(hits, pos); if (ret > temp) ret = temp; } return dp[x][y][pos] = ret; } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < (n); i++) scanf("%d", &t[i]); for (int i = 0; i < (n); i++) t[i]++; solution res = f(t[0], t[1], 1); printf("%d\n", res.moves); for (int i = 0; i < (n); i++) for (int j = 0; j < (res.all[i]); j++) printf("%d ", i + 1); printf("\n"); } ```
#include <bits/stdc++.h> using namespace std; int n, A, B; int a[11]; int ans = (size_t) "skc"; vector<int> tmp, anstmp; void dfs(int step, int cnt) { if (cnt >= ans) return; if (step == n - 1) { int x = max((a[step] + A) / A, (a[step + 1] + B) / B); x = max(x, (a[step - 1] + B) / B); tmp[step] = x; cnt += x; if (cnt < ans) { ans = cnt; anstmp = tmp; } tmp[step] = 0; return; } int x = max(0, (a[step - 1] + B) / B); tmp[step] = x; a[step - 1] -= x * B; a[step] -= x * A; a[step + 1] -= x * B; int i; for (i = 0;; ++i) { dfs(step + 1, cnt + x); if (a[step] < 0) break; ++x; ++tmp[step]; a[step - 1] -= B; a[step] -= A; a[step + 1] -= B; } a[step - 1] += x * B; a[step] += x * A; a[step + 1] += x * B; tmp[step] -= x; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> A >> B; tmp.resize(n + 1); int i; for (i = 1; i <= n; ++i) cin >> a[i]; dfs(2, 0); int j; cout << ans << '\n'; for (i = 2; i < n; ++i) { for (j = 1; j <= anstmp[i]; ++j) { cout << i << ' '; } } cout << '\n'; return 0; }
### Prompt Please create a solution in CPP to the following problem: This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, A, B; int a[11]; int ans = (size_t) "skc"; vector<int> tmp, anstmp; void dfs(int step, int cnt) { if (cnt >= ans) return; if (step == n - 1) { int x = max((a[step] + A) / A, (a[step + 1] + B) / B); x = max(x, (a[step - 1] + B) / B); tmp[step] = x; cnt += x; if (cnt < ans) { ans = cnt; anstmp = tmp; } tmp[step] = 0; return; } int x = max(0, (a[step - 1] + B) / B); tmp[step] = x; a[step - 1] -= x * B; a[step] -= x * A; a[step + 1] -= x * B; int i; for (i = 0;; ++i) { dfs(step + 1, cnt + x); if (a[step] < 0) break; ++x; ++tmp[step]; a[step - 1] -= B; a[step] -= A; a[step + 1] -= B; } a[step - 1] += x * B; a[step] += x * A; a[step + 1] += x * B; tmp[step] -= x; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> A >> B; tmp.resize(n + 1); int i; for (i = 1; i <= n; ++i) cin >> a[i]; dfs(2, 0); int j; cout << ans << '\n'; for (i = 2; i < n; ++i) { for (j = 1; j <= anstmp[i]; ++j) { cout << i << ' '; } } cout << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { ios_base::sync_with_stdio(false); cin >> n >> s; if (s[3] >= '6') s[3] = '0'; if (n == 12) { if (s[0] != '0' && s[0] != '1' && s[1] == '0') s[0] = '1'; else if (s[0] != '0' && s[0] != '1') s[0] = '0'; if (s[1] > '2' && s[0] == '1' || (s[0] == '0' && s[1] == '0')) s[1] = '2'; } if (n == 24) { if (s[0] != 0 && s[0] != '1' && s[0] != '2') s[0] = '0'; if (s[1] > '3' && s[0] == '2') s[1] = '3'; } cout << s; return 0; }
### Prompt Generate a Cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; int main() { ios_base::sync_with_stdio(false); cin >> n >> s; if (s[3] >= '6') s[3] = '0'; if (n == 12) { if (s[0] != '0' && s[0] != '1' && s[1] == '0') s[0] = '1'; else if (s[0] != '0' && s[0] != '1') s[0] = '0'; if (s[1] > '2' && s[0] == '1' || (s[0] == '0' && s[1] == '0')) s[1] = '2'; } if (n == 24) { if (s[0] != 0 && s[0] != '1' && s[0] != '2') s[0] = '0'; if (s[1] > '3' && s[0] == '2') s[1] = '3'; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf_int = 2e9; const long long inf_ll = 2e18; int Gcd(int p, int q) { return q == 0 ? p : Gcd(q, p % q); } int Pow(int p, int q) { int ans = 1; while (q) { if (q & 1) ans = ans * p; p = p * p; q >>= 1; } return ans; } inline int read() { int ra, fh; char rx; rx = getchar(), ra = 0, fh = 1; while ((rx < '0' || rx > '9') && rx != '-') rx = getchar(); if (rx == '-') fh = -1, rx = getchar(); while (rx >= '0' && rx <= '9') ra *= 10, ra += rx - 48, rx = getchar(); return ra * fh; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; n = read(); int a, b; scanf("%d:%d", &a, &b); if (a > n) a %= 10; if (n == 24 && a == 24) a %= 10; if (n == 12 && a == 0) a = 10; if (b >= 60) b %= 10; if (a < 10 && b < 10) printf("0%d:0%d\n", a, b); else if (b < 10) printf("%d:0%d\n", a, b); else if (a < 10) printf("0%d:%d\n", a, b); else printf("%d:%d\n", a, b); return 0; }
### Prompt Please create a solution in cpp to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf_int = 2e9; const long long inf_ll = 2e18; int Gcd(int p, int q) { return q == 0 ? p : Gcd(q, p % q); } int Pow(int p, int q) { int ans = 1; while (q) { if (q & 1) ans = ans * p; p = p * p; q >>= 1; } return ans; } inline int read() { int ra, fh; char rx; rx = getchar(), ra = 0, fh = 1; while ((rx < '0' || rx > '9') && rx != '-') rx = getchar(); if (rx == '-') fh = -1, rx = getchar(); while (rx >= '0' && rx <= '9') ra *= 10, ra += rx - 48, rx = getchar(); return ra * fh; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; n = read(); int a, b; scanf("%d:%d", &a, &b); if (a > n) a %= 10; if (n == 24 && a == 24) a %= 10; if (n == 12 && a == 0) a = 10; if (b >= 60) b %= 10; if (a < 10 && b < 10) printf("0%d:0%d\n", a, b); else if (b < 10) printf("%d:0%d\n", a, b); else if (a < 10) printf("0%d:%d\n", a, b); else printf("%d:%d\n", a, b); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int f; cin >> f; string s; cin >> s; if (f == 12) { if (s[3] > '5') s[3] = '0'; if (s[0] > '1') { if (s[1] == '0') s[0] = '1'; else s[0] = '0'; } if (s[0] == '1') { if (s[1] > '2') s[0] = '0'; } if (s[0] == '0' and s[1] == '0') s[1] = '1'; } if (f == 24) { if (s[3] > '5') s[3] = '0'; if (s[0] > '2') { s[0] = '0'; } if (s[0] == '2') { if (s[1] >= '4') s[0] = '0'; } } cout << s; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int f; cin >> f; string s; cin >> s; if (f == 12) { if (s[3] > '5') s[3] = '0'; if (s[0] > '1') { if (s[1] == '0') s[0] = '1'; else s[0] = '0'; } if (s[0] == '1') { if (s[1] > '2') s[0] = '0'; } if (s[0] == '0' and s[1] == '0') s[1] = '1'; } if (f == 24) { if (s[3] > '5') s[3] = '0'; if (s[0] > '2') { s[0] = '0'; } if (s[0] == '2') { if (s[1] >= '4') s[0] = '0'; } } cout << s; } ```
#include <bits/stdc++.h> using namespace std; void time12() { int a, b; char ch; cin >> a >> ch >> b; if (a >= 13) { if (a / 10 == 1) a = a - a % 10 + 2; else a = a % 10; } if (a == 0) a = 10; if (b >= 60) b = b % 10; cout << setfill('0') << setw(2) << a << ':' << setfill('0') << setw(2) << b; } void time24() { int a, b; char ch; cin >> a >> ch >> b; if (a >= 24) { if (a / 10 == 2) a = a - a % 10 + 3; else a = a % 10; } if (b >= 60) b = b % 10; cout << setfill('0') << setw(2) << a << ':' << setfill('0') << setw(2) << b; } int main() { int t; cin >> t; if (t == 12) time12(); else time24(); return 0; }
### Prompt Please formulate a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void time12() { int a, b; char ch; cin >> a >> ch >> b; if (a >= 13) { if (a / 10 == 1) a = a - a % 10 + 2; else a = a % 10; } if (a == 0) a = 10; if (b >= 60) b = b % 10; cout << setfill('0') << setw(2) << a << ':' << setfill('0') << setw(2) << b; } void time24() { int a, b; char ch; cin >> a >> ch >> b; if (a >= 24) { if (a / 10 == 2) a = a - a % 10 + 3; else a = a % 10; } if (b >= 60) b = b % 10; cout << setfill('0') << setw(2) << a << ':' << setfill('0') << setw(2) << b; } int main() { int t; cin >> t; if (t == 12) time12(); else time24(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, h, m; scanf("%d", &t); scanf("%d:%d", &h, &m); if (t == 12 && h == 0) h = 1; if (t == 24) t--; if (h > t || h < 10) { h %= 10; if (t == 12 && h == 0) printf("10:"); else printf("0%d:", h); } else printf("%d:", h); if (m > 59 || m < 10) { m %= 10; printf("0%d", m); } else printf("%d", m); return 0; }
### Prompt In CPP, your task is to solve the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, h, m; scanf("%d", &t); scanf("%d:%d", &h, &m); if (t == 12 && h == 0) h = 1; if (t == 24) t--; if (h > t || h < 10) { h %= 10; if (t == 12 && h == 0) printf("10:"); else printf("0%d:", h); } else printf("%d:", h); if (m > 59 || m < 10) { m %= 10; printf("0%d", m); } else printf("%d", m); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } template <class T> inline void chkmin(T &a, T b) { if (b < a) a = b; } int main() { int type, h, m; scanf("%d", &type); scanf("%d:%d", &h, &m); if (m >= 60) m %= 10; if (type == 12) { if (h == 0) { h = 1; } if (h > 12) { h = (h + 9) % 10 + 1; } } if (type == 24) { if (h >= 24) { h %= 10; } } printf("%d%d:%d%d\n", h / 10, h % 10, m / 10, m % 10); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } template <class T> inline void chkmin(T &a, T b) { if (b < a) a = b; } int main() { int type, h, m; scanf("%d", &type); scanf("%d:%d", &h, &m); if (m >= 60) m %= 10; if (type == 12) { if (h == 0) { h = 1; } if (h > 12) { h = (h + 9) % 10 + 1; } } if (type == 24) { if (h >= 24) { h %= 10; } } printf("%d%d:%d%d\n", h / 10, h % 10, m / 10, m % 10); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int h, m, n; scanf("%d %d:%d", &n, &h, &m); if (m >= 60) m %= 10; if (n == 12 && (h > 12 || h == 0)) { if (h % 10 == 0) h = 10; else h = h % 10; } else { if (n == 24 && h >= 24) { h = h % 10; } } printf("%02d:%02d", h, m); return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int h, m, n; scanf("%d %d:%d", &n, &h, &m); if (m >= 60) m %= 10; if (n == 12 && (h > 12 || h == 0)) { if (h % 10 == 0) h = 10; else h = h % 10; } else { if (n == 24 && h >= 24) { h = h % 10; } } printf("%02d:%02d", h, m); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string t; cin >> n >> t; if (n == 12) { if (t[0] == '1' && t[1] > '2') { t[0] = '0'; } else if (t[0] > '1') { if (t[1] == '0') t[0] = '1'; else t[0] = '0'; } else if (t[0] == '0' && t[1] == '0') { t[1] = '1'; } if (t[3] > '5') { t[3] = '0'; } } else { if ((t[0] == '2' && t[1] > '3') || t[0] > '2') { t[0] = '0'; } if (t[3] > '5') { t[3] = '0'; } } cout << t << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string t; cin >> n >> t; if (n == 12) { if (t[0] == '1' && t[1] > '2') { t[0] = '0'; } else if (t[0] > '1') { if (t[1] == '0') t[0] = '1'; else t[0] = '0'; } else if (t[0] == '0' && t[1] == '0') { t[1] = '1'; } if (t[3] > '5') { t[3] = '0'; } } else { if ((t[0] == '2' && t[1] > '3') || t[0] > '2') { t[0] = '0'; } if (t[3] > '5') { t[3] = '0'; } } cout << t << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int diff, adiff; char answ[10]; char inp[10]; char gen[10]; void Test(int h, int m) { gen[0] = h / 10 + '0'; gen[1] = h % 10 + '0'; gen[2] = ':'; gen[3] = m / 10 + '0'; gen[4] = m % 10 + '0'; gen[5] = '\0'; int diff = 0; for (int i = 0; i < 5; i++) { if (gen[i] != inp[i]) diff++; } if (diff < adiff) { adiff = diff; for (int i = 0; i < 6; i++) { answ[i] = gen[i]; } } } int main() { int format; adiff = 10; scanf("%d", &format); scanf("%s", inp); if (format == 24) { for (int h = 0; h < 24; h++) { for (int m = 0; m < 60; m++) { Test(h, m); } } } else { for (int h = 1; h <= 12; h++) { for (int m = 0; m < 60; m++) { Test(h, m); } } } printf("%s\n", answ); return 0; }
### Prompt Please create a solution in CPP to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int diff, adiff; char answ[10]; char inp[10]; char gen[10]; void Test(int h, int m) { gen[0] = h / 10 + '0'; gen[1] = h % 10 + '0'; gen[2] = ':'; gen[3] = m / 10 + '0'; gen[4] = m % 10 + '0'; gen[5] = '\0'; int diff = 0; for (int i = 0; i < 5; i++) { if (gen[i] != inp[i]) diff++; } if (diff < adiff) { adiff = diff; for (int i = 0; i < 6; i++) { answ[i] = gen[i]; } } } int main() { int format; adiff = 10; scanf("%d", &format); scanf("%s", inp); if (format == 24) { for (int h = 0; h < 24; h++) { for (int m = 0; m < 60; m++) { Test(h, m); } } } else { for (int h = 1; h <= 12; h++) { for (int m = 0; m < 60; m++) { Test(h, m); } } } printf("%s\n", answ); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int h = (s[0] - '0') * 10 + s[1] - '0'; int m = (s[3] - '0') * 10 + s[4] - '0'; if (n == 12) { if (h == 0) s[1] = '1'; else if (h % 10 == 0) s[0] = '1'; else if (h > 12) s[0] = '0'; if (m >= 60) s[3] = '0'; } else { if (h >= 24) s[0] = '0'; if (m >= 60) s[3] = '0'; } cout << s; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int h = (s[0] - '0') * 10 + s[1] - '0'; int m = (s[3] - '0') * 10 + s[4] - '0'; if (n == 12) { if (h == 0) s[1] = '1'; else if (h % 10 == 0) s[0] = '1'; else if (h > 12) s[0] = '0'; if (m >= 60) s[3] = '0'; } else { if (h >= 24) s[0] = '0'; if (m >= 60) s[3] = '0'; } cout << s; } ```
#include <bits/stdc++.h> using namespace std; void smain(); int main() { ios_base::sync_with_stdio(0); smain(); return 0; } long long n; inline long long dist(const string &a, const string &b) { long long res = 0; for (long long i = 0; i < 5; i++) res += a[i] != b[i]; return res; } void smain() { string s; for (; cin >> n >> s;) { string res = s; long long v = 10101; long long a = n == 12 ? 1 : 0, b = n == 12 ? 12 : 23; for (long long h = a; h <= b; ++h) { string hs = to_string(h); if (hs.length() < 2) hs = "0" + hs; for (long long m = 0; m < 60; ++m) { string ms = to_string(m); if (ms.length() < 2) ms = "0" + ms; string t = hs + ":" + ms; long long d = dist(s, t); if (d < v) v = d, res = t; } } cout << res << endl; } }
### Prompt Your task is to create a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void smain(); int main() { ios_base::sync_with_stdio(0); smain(); return 0; } long long n; inline long long dist(const string &a, const string &b) { long long res = 0; for (long long i = 0; i < 5; i++) res += a[i] != b[i]; return res; } void smain() { string s; for (; cin >> n >> s;) { string res = s; long long v = 10101; long long a = n == 12 ? 1 : 0, b = n == 12 ? 12 : 23; for (long long h = a; h <= b; ++h) { string hs = to_string(h); if (hs.length() < 2) hs = "0" + hs; for (long long m = 0; m < 60; ++m) { string ms = to_string(m); if (ms.length() < 2) ms = "0" + ms; string t = hs + ":" + ms; long long d = dist(s, t); if (d < v) v = d, res = t; } } cout << res << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int type; string line; cin >> type >> line; string h, m; h += line[0]; h += line[1]; m += line[3]; m += line[4]; if (type == 12) { if (m[0] <= '5') { } else if (m[0] > '5') { m[0] = '0'; } if (h[0] == '0') { if (h[1] == '0') h[1] = '1'; } else if (h[0] == '1') { if (h[1] == '0' || h[1] == '1' || h[1] == '2') { } else { h[0] = '0'; } } else { if (h[1] == '0') { h[0] = '1'; } else { h[0] = '0'; } } } else if (type == 24) { if (m[0] <= '5') { } else if (m[0] > '5') { m[0] = '0'; } if (h[0] == '0') { } else if (h[0] == '1') { } else if (h[0] == '2') { if (h[1] == '0' || h[1] == '1' || h[1] == '2' || h[1] == '3') { } else { h[1] = '0'; } } else { h[0] = '0'; } } cout << h << ':' << m << '\n'; }
### Prompt In cpp, your task is to solve the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int type; string line; cin >> type >> line; string h, m; h += line[0]; h += line[1]; m += line[3]; m += line[4]; if (type == 12) { if (m[0] <= '5') { } else if (m[0] > '5') { m[0] = '0'; } if (h[0] == '0') { if (h[1] == '0') h[1] = '1'; } else if (h[0] == '1') { if (h[1] == '0' || h[1] == '1' || h[1] == '2') { } else { h[0] = '0'; } } else { if (h[1] == '0') { h[0] = '1'; } else { h[0] = '0'; } } } else if (type == 24) { if (m[0] <= '5') { } else if (m[0] > '5') { m[0] = '0'; } if (h[0] == '0') { } else if (h[0] == '1') { } else if (h[0] == '2') { if (h[1] == '0' || h[1] == '1' || h[1] == '2' || h[1] == '3') { } else { h[1] = '0'; } } else { h[0] = '0'; } } cout << h << ':' << m << '\n'; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n; cin >> n; char h[5]; for (int i = 0; i < 5; i++) cin >> h[i]; if (n == 24) { bool bad = false; if (h[0] > '2') h[0] = '0'; if (h[0] == '2') bad = true; if (h[1] > '3' && bad) h[1] = '0'; } else { bool bad = false; if (h[0] > '1') { if (h[1] == '0') h[0] = '1'; else h[0] = '0'; } if (h[0] == '1') bad = true; if (h[1] > '2' && bad) h[1] = '1'; if (h[0] == '0' && h[1] == '0') h[0] = '1'; } if (h[3] > '5') h[3] = '0'; for (int i = 0; i < 5; i++) cout << h[i]; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n; cin >> n; char h[5]; for (int i = 0; i < 5; i++) cin >> h[i]; if (n == 24) { bool bad = false; if (h[0] > '2') h[0] = '0'; if (h[0] == '2') bad = true; if (h[1] > '3' && bad) h[1] = '0'; } else { bool bad = false; if (h[0] > '1') { if (h[1] == '0') h[0] = '1'; else h[0] = '0'; } if (h[0] == '1') bad = true; if (h[1] > '2' && bad) h[1] = '1'; if (h[0] == '0' && h[1] == '0') h[0] = '1'; } if (h[3] > '5') h[3] = '0'; for (int i = 0; i < 5; i++) cout << h[i]; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); int n; cin >> n; string s; cin >> s; if (n == 12) { if (s[0] == '0' and s[1] == '0') s[1] = '1'; else if (s[0] > '1') { if (s[1] == '0') s[0] = '1'; else s[0] = '0'; } else if (s[0] == '1' && s[1] > '2') s[1] = '1'; if (s[3] > '5') s[3] = '1'; } else { if (s[0] > '2') s[0] = '1'; else if (s[0] == '2' && s[1] > '3') s[1] = '0'; if (s[3] > '5') s[3] = '1'; } cout << s << endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); int n; cin >> n; string s; cin >> s; if (n == 12) { if (s[0] == '0' and s[1] == '0') s[1] = '1'; else if (s[0] > '1') { if (s[1] == '0') s[0] = '1'; else s[0] = '0'; } else if (s[0] == '1' && s[1] > '2') s[1] = '1'; if (s[3] > '5') s[3] = '1'; } else { if (s[0] > '2') s[0] = '1'; else if (s[0] == '2' && s[1] > '3') s[1] = '0'; if (s[3] > '5') s[3] = '1'; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:10034217728") using namespace std; int main() { int a; cin >> a; string s; cin >> s; string aa = string() + s[0] + s[1]; string b = string() + s[3] + s[4]; int h; sscanf(aa.c_str(), "%d", &h); if (a == 12) { if (h < 1 || h > 12) { if (aa[1] == '0') { aa[0] = '1'; } else { aa[0] = '0'; } } } if (a == 24) { if (h >= 24) { aa[0] = '0'; } } int m; sscanf(b.c_str(), "%d", &m); if (m > 59) b[0] = '1'; printf("%02s:%02s", aa.c_str(), b.c_str()); return 0; }
### Prompt Generate a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:10034217728") using namespace std; int main() { int a; cin >> a; string s; cin >> s; string aa = string() + s[0] + s[1]; string b = string() + s[3] + s[4]; int h; sscanf(aa.c_str(), "%d", &h); if (a == 12) { if (h < 1 || h > 12) { if (aa[1] == '0') { aa[0] = '1'; } else { aa[0] = '0'; } } } if (a == 24) { if (h >= 24) { aa[0] = '0'; } } int m; sscanf(b.c_str(), "%d", &m); if (m > 59) b[0] = '1'; printf("%02s:%02s", aa.c_str(), b.c_str()); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { int n; char ch[3] = { 0, }, cm[3] = { 0, }; scanf("%d", &n); scanf("%2s:%2s", ch, cm); int h = atoi(ch); int m = atoi(cm); if (n == 12) { if (h == 0) { ch[1] = '1'; } else if (h > 12) { if (ch[1] != '0') ch[0] = '0'; else ch[0] = '1'; } } else { if (h >= 24) { ch[0] = '0'; } } if (m >= 60) { cm[0] = '0'; } printf("%2s:%2s\n", ch, cm); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { int n; char ch[3] = { 0, }, cm[3] = { 0, }; scanf("%d", &n); scanf("%2s:%2s", ch, cm); int h = atoi(ch); int m = atoi(cm); if (n == 12) { if (h == 0) { ch[1] = '1'; } else if (h > 12) { if (ch[1] != '0') ch[0] = '0'; else ch[0] = '1'; } } else { if (h >= 24) { ch[0] = '0'; } } if (m >= 60) { cm[0] = '0'; } printf("%2s:%2s\n", ch, cm); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; string a = "", b = ""; int j = 0; while (s[j] != ':') { a = a + s[j]; j++; } j++; while (j != 5) { b += s[j]; j++; } int x = atoi(a.c_str()), y = atoi(b.c_str()); if (!(y >= 0 && y <= 59)) b[0] = '0'; if (n == 12) { if (x > 12) { if (a[1] == '0') { a[0] = '1'; } else a[0] = '0'; } else if (x == 0) a[0] = '1'; } else { if (x > 23) a[0] = '0'; } cout << a << ":" << b << endl; }
### Prompt Create a solution in Cpp for the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; string a = "", b = ""; int j = 0; while (s[j] != ':') { a = a + s[j]; j++; } j++; while (j != 5) { b += s[j]; j++; } int x = atoi(a.c_str()), y = atoi(b.c_str()); if (!(y >= 0 && y <= 59)) b[0] = '0'; if (n == 12) { if (x > 12) { if (a[1] == '0') { a[0] = '1'; } else a[0] = '0'; } else if (x == 0) a[0] = '1'; } else { if (x > 23) a[0] = '0'; } cout << a << ":" << b << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n == 12) { string hour = ""; hour += s[0]; hour += s[1]; string min = ""; min += s[3]; min += s[4]; if (hour == "00") s[0] = '1'; if (stoi(hour) > 12) { if (s[1] == '0') s[0] = '1'; else if (s[1] == '2' || s[1] == '1') s[0] = '1'; else { s[0] = '0'; } } if (stoi(min) > 59) { s[3] = '0'; } } else { string hour = ""; hour += s[0]; hour += s[1]; string min = ""; min += s[3]; min += s[4]; if (stoi(hour) > 23) { s[0] = '0'; } if (stoi(min) > 59) s[3] = '0'; } cout << s << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n == 12) { string hour = ""; hour += s[0]; hour += s[1]; string min = ""; min += s[3]; min += s[4]; if (hour == "00") s[0] = '1'; if (stoi(hour) > 12) { if (s[1] == '0') s[0] = '1'; else if (s[1] == '2' || s[1] == '1') s[0] = '1'; else { s[0] = '0'; } } if (stoi(min) > 59) { s[3] = '0'; } } else { string hour = ""; hour += s[0]; hour += s[1]; string min = ""; min += s[3]; min += s[4]; if (stoi(hour) > 23) { s[0] = '0'; } if (stoi(min) > 59) s[3] = '0'; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; char str[6]; cin >> n; cin >> str; if (n == 12) { if (str[0] == '0' && str[1] == '0') str[1] = '1'; else if (str[0] > '1' && str[1] > '0') str[0] = '0'; else if (str[0] > '1' && str[1] == '0') str[0] = '1'; else if (str[0] == '1' && str[1] > '2') str[1] = '1'; } else if (n == 24) { if (str[0] > '2') str[0] = '0'; else if (str[0] == '2' && str[1] > '3') str[0] = '1'; } if (str[3] > '5') str[3] = '5'; cout << str; return 0; }
### Prompt Create a solution in Cpp for the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; char str[6]; cin >> n; cin >> str; if (n == 12) { if (str[0] == '0' && str[1] == '0') str[1] = '1'; else if (str[0] > '1' && str[1] > '0') str[0] = '0'; else if (str[0] > '1' && str[1] == '0') str[0] = '1'; else if (str[0] == '1' && str[1] > '2') str[1] = '1'; } else if (n == 24) { if (str[0] > '2') str[0] = '0'; else if (str[0] == '2' && str[1] > '3') str[0] = '1'; } if (str[3] > '5') str[3] = '5'; cout << str; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n == 12) { int d1 = (s[0] - 48) * 10 + s[1] - 48; if (d1 == 0) { s[1] = '1'; } else if (d1 >= 13 && s[1] == '0') s[0] = '1'; else if (d1 >= 13) s[0] = '0'; int d2 = (s[3] - 48) * 10 + s[4] - 48; if (d2 >= 60) s[3] = '0'; cout << s; } else { int d1 = (s[0] - 48) * 10 + s[1] - 48; if (d1 >= 24) s[0] = '1'; int d2 = (s[3] - 48) * 10 + s[4] - 48; if (d2 >= 60) s[3] = '0'; cout << s; } return 0; }
### Prompt Generate a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n == 12) { int d1 = (s[0] - 48) * 10 + s[1] - 48; if (d1 == 0) { s[1] = '1'; } else if (d1 >= 13 && s[1] == '0') s[0] = '1'; else if (d1 >= 13) s[0] = '0'; int d2 = (s[3] - 48) * 10 + s[4] - 48; if (d2 >= 60) s[3] = '0'; cout << s; } else { int d1 = (s[0] - 48) * 10 + s[1] - 48; if (d1 >= 24) s[0] = '1'; int d2 = (s[3] - 48) * 10 + s[4] - 48; if (d2 >= 60) s[3] = '0'; cout << s; } return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; int n; int main() { cin >> n >> s; int c = s[3] - '0'; if (c > 5) s[3] = '0'; int a = s[0] - '0'; int b = s[1] - '0'; if (n == 12) { if (a == 0) { if (b == 0) s[1] = '1'; } else if (a == 1) { if (b > 2) s[1] = '0'; } else { if (b == 0) s[0] = '1'; else s[0] = '0'; } } else { if (a == 2) { if (b > 3) s[1] = '0'; } if (a > 2) s[0] = '0'; } cout << s << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int n; int main() { cin >> n >> s; int c = s[3] - '0'; if (c > 5) s[3] = '0'; int a = s[0] - '0'; int b = s[1] - '0'; if (n == 12) { if (a == 0) { if (b == 0) s[1] = '1'; } else if (a == 1) { if (b > 2) s[1] = '0'; } else { if (b == 0) s[0] = '1'; else s[0] = '0'; } } else { if (a == 2) { if (b > 3) s[1] = '0'; } if (a > 2) s[0] = '0'; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n = 0; string s = ""; int main() { cin >> n >> s; if (n == 12) { if (s.substr(0, 2) == "00") s[1] = '1'; else if (s.substr(0, 2) > "12") s[0] = s[1] == '0' ? '1' : '0'; } else if (s.substr(0, 2) > "23") s[0] = '0'; if (s.substr(3, 5) > "59") s[3] = '0'; cout << s; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n = 0; string s = ""; int main() { cin >> n >> s; if (n == 12) { if (s.substr(0, 2) == "00") s[1] = '1'; else if (s.substr(0, 2) > "12") s[0] = s[1] == '0' ? '1' : '0'; } else if (s.substr(0, 2) > "23") s[0] = '0'; if (s.substr(3, 5) > "59") s[3] = '0'; cout << s; } ```
#include <bits/stdc++.h> using namespace std; int main() { int format; cin >> format; char time[8]; cin >> time; if (format == 12) { if (time[3] > '5') { time[3] = '0'; } if (time[0] == '0') { if (time[1] == '0') { time[1] = '1'; } } else if (time[0] > '1' || time[1] > '2') { if (time[1] != '0') { time[0] = '0'; } else { time[0] = '1'; } } } else { if (time[3] > '5') { time[3] = '0'; } if (time[0] > '2') { time[0] = '0'; } else if (time[0] == '2' && time[1] > '3') { time[1] = '0'; } } cout << time << endl; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int format; cin >> format; char time[8]; cin >> time; if (format == 12) { if (time[3] > '5') { time[3] = '0'; } if (time[0] == '0') { if (time[1] == '0') { time[1] = '1'; } } else if (time[0] > '1' || time[1] > '2') { if (time[1] != '0') { time[0] = '0'; } else { time[0] = '1'; } } } else { if (time[3] > '5') { time[3] = '0'; } if (time[0] > '2') { time[0] = '0'; } else if (time[0] == '2' && time[1] > '3') { time[1] = '0'; } } cout << time << endl; } ```
#include <bits/stdc++.h> using namespace std; char h1, h2, m1, m2; int format; int main() { scanf("%d\n%c%c:%c%c", &format, &h1, &h2, &m1, &m2); if (format == 24) { if ((h1 >= '2') && (h2 >= '4')) h1 = '0'; if (h1 > '2') h1 = '0'; } else { if ((h1 == '1') && (h2 >= '3')) h1 = '0'; if ((h1 == '0') && (h2 == '0')) h2 = '1'; if ((h1 >= '2') && (h2 == '0')) h1 = '1'; if ((h1 >= '2') && (h2 >= '1')) h1 = '0'; } if (m1 >= '6') m1 = '0'; printf("%c%c:%c%c", h1, h2, m1, m2); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char h1, h2, m1, m2; int format; int main() { scanf("%d\n%c%c:%c%c", &format, &h1, &h2, &m1, &m2); if (format == 24) { if ((h1 >= '2') && (h2 >= '4')) h1 = '0'; if (h1 > '2') h1 = '0'; } else { if ((h1 == '1') && (h2 >= '3')) h1 = '0'; if ((h1 == '0') && (h2 == '0')) h2 = '1'; if ((h1 >= '2') && (h2 == '0')) h1 = '1'; if ((h1 >= '2') && (h2 >= '1')) h1 = '0'; } if (m1 >= '6') m1 = '0'; printf("%c%c:%c%c", h1, h2, m1, m2); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string time; cin >> time; if (t == 24) { if (time[3] > '5') time[3] = '0'; if (time[0] != '0' && time[0] != '1' && time[0] != '2') time[0] = '0'; if (time[0] == '2' && !(time[1] >= '0' && time[1] <= '3')) time[1] = '0'; } else { if (time[3] > '5') time[3] = '0'; if (time[0] > '1' && time[1] == '0') time[0] = '1'; if (time[0] > '1') time[0] = '0'; if (time[0] == '0' && time[1] == '0') time[1] = '1'; if (time[0] == '1' && time[1] > '2') time[1] = '1'; } cout << time << endl; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string time; cin >> time; if (t == 24) { if (time[3] > '5') time[3] = '0'; if (time[0] != '0' && time[0] != '1' && time[0] != '2') time[0] = '0'; if (time[0] == '2' && !(time[1] >= '0' && time[1] <= '3')) time[1] = '0'; } else { if (time[3] > '5') time[3] = '0'; if (time[0] > '1' && time[1] == '0') time[0] = '1'; if (time[0] > '1') time[0] = '0'; if (time[0] == '0' && time[1] == '0') time[1] = '1'; if (time[0] == '1' && time[1] > '2') time[1] = '1'; } cout << time << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& o, vector<T>& v) { for (auto& x : v) o << x << ' '; return o; } int diff(int a, int b) { string A = to_string(a); string B = to_string(b); if ((int)A.size() == 1) A = string(1, '0') + A; if ((int)B.size() == 1) B = string(1, '0') + B; int res = 0; for (int i = 0; i < 2; i++) { if (A[i] != B[i]) res++; } return res; } string pad(int a) { string A = to_string(a); if ((int)A.size() == 1) A = string(1, '0') + A; return A; } int main() { std::ios_base::sync_with_stdio(false); int typ; cin >> typ; int hh, mm; char cc; cin >> hh >> cc >> mm; int res = INT_MAX; int hans, mans; if (typ == 12) { for (int hour = 1; hour <= 12; hour++) { for (int minn = 0; minn <= 59; minn++) { int cur = diff(hour, hh) + diff(mm, minn); if (cur < res) { res = cur; hans = hour; mans = minn; } } } } else { for (int hour = 0; hour <= 23; hour++) { for (int minn = 0; minn <= 59; minn++) { int cur = diff(hour, hh) + diff(mm, minn); if (cur < res) { res = cur; hans = hour; mans = minn; } } } } cout << pad(hans) << ":" << pad(mans) << endl; }
### Prompt Generate a Cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& o, vector<T>& v) { for (auto& x : v) o << x << ' '; return o; } int diff(int a, int b) { string A = to_string(a); string B = to_string(b); if ((int)A.size() == 1) A = string(1, '0') + A; if ((int)B.size() == 1) B = string(1, '0') + B; int res = 0; for (int i = 0; i < 2; i++) { if (A[i] != B[i]) res++; } return res; } string pad(int a) { string A = to_string(a); if ((int)A.size() == 1) A = string(1, '0') + A; return A; } int main() { std::ios_base::sync_with_stdio(false); int typ; cin >> typ; int hh, mm; char cc; cin >> hh >> cc >> mm; int res = INT_MAX; int hans, mans; if (typ == 12) { for (int hour = 1; hour <= 12; hour++) { for (int minn = 0; minn <= 59; minn++) { int cur = diff(hour, hh) + diff(mm, minn); if (cur < res) { res = cur; hans = hour; mans = minn; } } } } else { for (int hour = 0; hour <= 23; hour++) { for (int minn = 0; minn <= 59; minn++) { int cur = diff(hour, hh) + diff(mm, minn); if (cur < res) { res = cur; hans = hour; mans = minn; } } } } cout << pad(hans) << ":" << pad(mans) << endl; } ```
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> void _max(T &a, U b) { if (a < b) a = b; } template <typename T, typename U> void _min(T &a, U b) { if (a > b) a = b; } const int N = 100005; long long power(long long n, long long p) { long long x = 1; while (p) { if (p % 2) x = (x * n) % 1000000007; if (p /= 2) n = (n * n) % 1000000007; } return x; } int main() { long long n, i, j, ans = 0; string s; cin >> n >> s; if (n == 24) { if ((s[0] > '1' && s[1] > '3') || s[0] > '2') { s[0] = '1'; } } else if (n == 12) { if (s[0] == '1' && s[1] > '2') { s[0] = '0'; } else if (s[0] > '1') { if (s[1] != '0') { s[0] = '0'; } else s[0] = '1'; } else if (s[0] == '0' && s[1] == '0') { s[1] = '1'; } } if (s[3] > '5') s[3] = '0'; cout << s << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T, typename U> void _max(T &a, U b) { if (a < b) a = b; } template <typename T, typename U> void _min(T &a, U b) { if (a > b) a = b; } const int N = 100005; long long power(long long n, long long p) { long long x = 1; while (p) { if (p % 2) x = (x * n) % 1000000007; if (p /= 2) n = (n * n) % 1000000007; } return x; } int main() { long long n, i, j, ans = 0; string s; cin >> n >> s; if (n == 24) { if ((s[0] > '1' && s[1] > '3') || s[0] > '2') { s[0] = '1'; } } else if (n == 12) { if (s[0] == '1' && s[1] > '2') { s[0] = '0'; } else if (s[0] > '1') { if (s[1] != '0') { s[0] = '0'; } else s[0] = '1'; } else if (s[0] == '0' && s[1] == '0') { s[1] = '1'; } } if (s[3] > '5') s[3] = '0'; cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long delbit(long long a, long long k) { return a & (~(1 << k)); } int delbit(int a, int k) { return a & (~(1 << k)); } bool getbit(long long a, long long k) { return 1 & (a >> k); } bool getbit(int a, int k) { return 1 & (a >> k); } long long setbit(long long a, long long k) { return a |= (1 << k); } int setbit(int a, int k) { return a |= (1 << k); } inline long long mulmod(long long first, long long n, long long _mod) { long long res = 0; while (n) { if (n & 1) res = (res + first) % _mod; first = (first + first) % _mod; n >>= 1; } return res; } inline long long powmod(long long first, long long n, long long _mod) { long long res = 1; while (n) { if (n & 1) res = (res * first) % _mod; first = (first * first) % _mod; n >>= 1; } return res; } inline long long gcd(long long a, long long b) { long long t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline int gcd(int a, int b) { int t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long gcd(long long a, long long b, long long c) { return gcd(gcd(a, b), c); } inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } double dist(double ax, double ay, double bx, double by) { return sqrt(((ax - bx) * (ax - bx)) + ((ay - by) * (ay - by))); } template <typename T, typename T2> void printarr(T a[], T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; scanf("%d", &n); int h, m; scanf("%d:%d", &h, &m); int from, to; if (n == 12) { from = 1; to = 13; } else { from = 0; to = 24; } int mn = 1011111111; pair<int, int> ans; for (int(i) = (from); (i) < (to); (i)++) { for (int(j) = (0); (j) < (60); (j)++) { int cur = 0; if (h % 10 != i % 10) cur++; if ((h / 10) % 10 != (i / 10) % 10) cur++; if (m % 10 != j % 10) cur++; if ((m / 10) % 10 != (j / 10) % 10) cur++; if (mn > cur) { ans = {i, j}; mn = cur; } } } printf("%02d:%02d\n", ans.first, ans.second); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long delbit(long long a, long long k) { return a & (~(1 << k)); } int delbit(int a, int k) { return a & (~(1 << k)); } bool getbit(long long a, long long k) { return 1 & (a >> k); } bool getbit(int a, int k) { return 1 & (a >> k); } long long setbit(long long a, long long k) { return a |= (1 << k); } int setbit(int a, int k) { return a |= (1 << k); } inline long long mulmod(long long first, long long n, long long _mod) { long long res = 0; while (n) { if (n & 1) res = (res + first) % _mod; first = (first + first) % _mod; n >>= 1; } return res; } inline long long powmod(long long first, long long n, long long _mod) { long long res = 1; while (n) { if (n & 1) res = (res * first) % _mod; first = (first * first) % _mod; n >>= 1; } return res; } inline long long gcd(long long a, long long b) { long long t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline int gcd(int a, int b) { int t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long gcd(long long a, long long b, long long c) { return gcd(gcd(a, b), c); } inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } double dist(double ax, double ay, double bx, double by) { return sqrt(((ax - bx) * (ax - bx)) + ((ay - by) * (ay - by))); } template <typename T, typename T2> void printarr(T a[], T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; scanf("%d", &n); int h, m; scanf("%d:%d", &h, &m); int from, to; if (n == 12) { from = 1; to = 13; } else { from = 0; to = 24; } int mn = 1011111111; pair<int, int> ans; for (int(i) = (from); (i) < (to); (i)++) { for (int(j) = (0); (j) < (60); (j)++) { int cur = 0; if (h % 10 != i % 10) cur++; if ((h / 10) % 10 != (i / 10) % 10) cur++; if (m % 10 != j % 10) cur++; if ((m / 10) % 10 != (j / 10) % 10) cur++; if (mn > cur) { ans = {i, j}; mn = cur; } } } printf("%02d:%02d\n", ans.first, ans.second); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; int h1, h2, m1, m2; char ch; int t = 1; char s[6]; while (t--) { cin >> n; cin >> s; h1 = s[0] - '0'; h2 = s[1] - '0'; m1 = s[3] - '0'; m2 = s[4] - '0'; if (m1 * 10 + m2 > 59) { m1 = 0; } if (n == 12) { if ((h1 * 10 + h2) % 10 == 0) { h1 = 1; } else { if (h1 * 10 + h2 > 12) h1 = 0; } } if (n == 24) { if ((h1 * 10 + h2) % 10 == 0 && h1 > 2) { h1 = 1; } else { if (h1 * 10 + h2 > 23) h1 = 0; } } cout << h1 << h2 << ":" << m1 << m2 << "\n"; } }
### Prompt Please formulate a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; int h1, h2, m1, m2; char ch; int t = 1; char s[6]; while (t--) { cin >> n; cin >> s; h1 = s[0] - '0'; h2 = s[1] - '0'; m1 = s[3] - '0'; m2 = s[4] - '0'; if (m1 * 10 + m2 > 59) { m1 = 0; } if (n == 12) { if ((h1 * 10 + h2) % 10 == 0) { h1 = 1; } else { if (h1 * 10 + h2 > 12) h1 = 0; } } if (n == 24) { if ((h1 * 10 + h2) % 10 == 0 && h1 > 2) { h1 = 1; } else { if (h1 * 10 + h2 > 23) h1 = 0; } } cout << h1 << h2 << ":" << m1 << m2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int f, a, b; int main() { scanf("%d%d:%d", &f, &a, &b); if (b > 59) { b %= 10; } if (a > f || a == 24) { a %= 10; } if (a == 0 && f == 12) { a = 10; } printf("%02d:%02d", a, b); }
### Prompt Please provide a cpp coded solution to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int f, a, b; int main() { scanf("%d%d:%d", &f, &a, &b); if (b > 59) { b %= 10; } if (a > f || a == 24) { a %= 10; } if (a == 0 && f == 12) { a = 10; } printf("%02d:%02d", a, b); } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i, j; int f, h, m; scanf("%d", &f); scanf("%d:%d", &h, &m); if (f == 12) { if (m > 59) { if (m % 10 == 0) m = 0; else m = m % 10; } if (h > 12) { if (h % 10 == 0) h = 10; else h %= 10; } else if (h == 0) h = 1; } else { if (m > 59) { if (m % 10 == 0) m = 0; else m = m % 10; } if (h > 23) { h %= 10; } } printf("%02d:%02d\n", h, m); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, i, j; int f, h, m; scanf("%d", &f); scanf("%d:%d", &h, &m); if (f == 12) { if (m > 59) { if (m % 10 == 0) m = 0; else m = m % 10; } if (h > 12) { if (h % 10 == 0) h = 10; else h %= 10; } else if (h == 0) h = 1; } else { if (m > 59) { if (m % 10 == 0) m = 0; else m = m % 10; } if (h > 23) { h %= 10; } } printf("%02d:%02d\n", h, m); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int n, sum = 0, count = 0, m, flag = 0, ans = 0; cin >> n; string s; cin >> s; int a, b, c, d; if (n == 24) { a = s[0] - '0'; b = s[1] - '0'; c = s[3] - '0'; d = s[4] - '0'; if (a > 1) { if (a == 2) { if (b >= 4) b = 0; } else a = 0; } if (c > 5) c = 0; } else { a = s[0] - '0'; b = s[1] - '0'; c = s[3] - '0'; d = s[4] - '0'; if (a > 0) { if (a == 1) { if (b >= 3) b = 0; } else if (b == 0) a = 1; else a = 0; } else if (a == 0) { if (b == 0) b = 1; } if (c > 5) c = 0; } cout << a << b << ":" << c << d; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int n, sum = 0, count = 0, m, flag = 0, ans = 0; cin >> n; string s; cin >> s; int a, b, c, d; if (n == 24) { a = s[0] - '0'; b = s[1] - '0'; c = s[3] - '0'; d = s[4] - '0'; if (a > 1) { if (a == 2) { if (b >= 4) b = 0; } else a = 0; } if (c > 5) c = 0; } else { a = s[0] - '0'; b = s[1] - '0'; c = s[3] - '0'; d = s[4] - '0'; if (a > 0) { if (a == 1) { if (b >= 3) b = 0; } else if (b == 0) a = 1; else a = 0; } else if (a == 0) { if (b == 0) b = 1; } if (c > 5) c = 0; } cout << a << b << ":" << c << d; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a[1000005] = {}; char s[5]; int main() { long long t, m; cin >> t; cin >> s; for (int i = 4; i >= 2; i--) { int p = s[i] - 48; if (i == 3) { if (p >= 6) s[i] = 51; } } if (t == 24) { int p = s[0] - 48; int q = s[1] - 48; if (p > 2) { s[0] = 1 + 48; } else if (p == 2) { if (q > 3) s[1] = 0 + 48; } } if (t == 12) { int p = s[0] - 48; int q = s[1] - 48; if (p > 1) { if (q == 0) { s[0] = 1 + 48; } else s[0] = 0 + 48; } if (p == 1) { if (q > 2) s[1] = 0 + 48; } if (p == 0) { if (q == 0) { s[1] = 1 + 48; } } } cout << s << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[1000005] = {}; char s[5]; int main() { long long t, m; cin >> t; cin >> s; for (int i = 4; i >= 2; i--) { int p = s[i] - 48; if (i == 3) { if (p >= 6) s[i] = 51; } } if (t == 24) { int p = s[0] - 48; int q = s[1] - 48; if (p > 2) { s[0] = 1 + 48; } else if (p == 2) { if (q > 3) s[1] = 0 + 48; } } if (t == 12) { int p = s[0] - 48; int q = s[1] - 48; if (p > 1) { if (q == 0) { s[0] = 1 + 48; } else s[0] = 0 + 48; } if (p == 1) { if (q > 2) s[1] = 0 + 48; } if (p == 0) { if (q == 0) { s[1] = 1 + 48; } } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int mode; cin >> mode; string time; cin >> time; if (time[3] - '0' > 5) time[3] = '5'; int hour = (time[0] - '0') * 10 + (time[1] - '0'); if (hour > mode || (hour == mode && mode == 24)) { if (mode == 12) { if (time[1] != '0') time[0] = '0'; else time[0] = '1'; } else time[0] = '0'; } else if (hour == 0 && mode == 12) { time[0] = '1'; } cout << time; int de; cin >> de; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int mode; cin >> mode; string time; cin >> time; if (time[3] - '0' > 5) time[3] = '5'; int hour = (time[0] - '0') * 10 + (time[1] - '0'); if (hour > mode || (hour == mode && mode == 24)) { if (mode == 12) { if (time[1] != '0') time[0] = '0'; else time[0] = '1'; } else time[0] = '0'; } else if (hour == 0 && mode == 12) { time[0] = '1'; } cout << time; int de; cin >> de; } ```
#include <bits/stdc++.h> using namespace std; int main() { int ty; cin >> ty; int h, m; scanf("%d:%d", &h, &m); if (ty == 12) { if (h == 0) { h = 1; } else if (h > 12) { if (h % 10 == 0) { h = 10; } else { h = h % 10; } } } else { if (h >= 24) h = h % 10; } if (m >= 60) m = m % 10; if (h < 10) putchar('0'); printf("%d", h); putchar(':'); if (m < 10) putchar('0'); printf("%d\n", m); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int ty; cin >> ty; int h, m; scanf("%d:%d", &h, &m); if (ty == 12) { if (h == 0) { h = 1; } else if (h > 12) { if (h % 10 == 0) { h = 10; } else { h = h % 10; } } } else { if (h >= 24) h = h % 10; } if (m >= 60) m = m % 10; if (h < 10) putchar('0'); printf("%d", h); putchar(':'); if (m < 10) putchar('0'); printf("%d\n", m); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k; while (scanf("%d", &n) != EOF) { char a[5]; for (i = 0; i < 5; i++) { cin >> a[i]; } if (n == 12) { if ((a[0] > '1' && a[1] != '0') || (a[0] == '1' && a[1] > '2')) a[0] = '0'; if (a[0] == '0' && a[1] == '0') a[1] = '1'; if (a[0] > '1' && a[1] == '0') a[0] = '1'; if (a[3] >= '6') a[3] = '0'; for (k = 0; k < 5; k++) { cout << a[k]; } } else { if (a[0] > '2' || (a[0] == '2' && a[1] > '3')) a[0] = '0'; if (a[3] >= '6') a[3] = '0'; for (k = 0; k < 5; k++) { cout << a[k]; } } cout << "\n"; } return 0; }
### Prompt Please create a solution in cpp to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, i, k; while (scanf("%d", &n) != EOF) { char a[5]; for (i = 0; i < 5; i++) { cin >> a[i]; } if (n == 12) { if ((a[0] > '1' && a[1] != '0') || (a[0] == '1' && a[1] > '2')) a[0] = '0'; if (a[0] == '0' && a[1] == '0') a[1] = '1'; if (a[0] > '1' && a[1] == '0') a[0] = '1'; if (a[3] >= '6') a[3] = '0'; for (k = 0; k < 5; k++) { cout << a[k]; } } else { if (a[0] > '2' || (a[0] == '2' && a[1] > '3')) a[0] = '0'; if (a[3] >= '6') a[3] = '0'; for (k = 0; k < 5; k++) { cout << a[k]; } } cout << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int f; cin >> f; int h, m; char c; cin >> h >> c >> m; if (f == 12) { if (h > 12 and h % 10 == 0) h = 10; else if (h > 12) h = h % 10; if (h == 0) h = 1; } else { if (h > 23) h = h % 10; } if (m > 59) m = m % 10; if (h < 10) cout << '0'; cout << h << ":"; if (m < 10) cout << '0'; cout << m << endl; }
### Prompt Your task is to create a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int f; cin >> f; int h, m; char c; cin >> h >> c >> m; if (f == 12) { if (h > 12 and h % 10 == 0) h = 10; else if (h > 12) h = h % 10; if (h == 0) h = 1; } else { if (h > 23) h = h % 10; } if (m > 59) m = m % 10; if (h < 10) cout << '0'; cout << h << ":"; if (m < 10) cout << '0'; cout << m << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char ch[10]; scanf("%s", ch); int a = (ch[0] - '0') * 10 + ch[1] - '0'; int b = (ch[3] - '0') * 10 + ch[4] - '0'; if (n == 24) { if (a >= 24) { ch[0] = '0'; } if (b >= 60) ch[3] = '0'; } if (n == 12) { if (a > n) { if (ch[1] != '0') ch[0] = '0'; else { ch[0] = '1'; } } if (a == 0) ch[1] = '1'; if (b >= 60) ch[3] = '0'; } printf("%s\n", ch); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char ch[10]; scanf("%s", ch); int a = (ch[0] - '0') * 10 + ch[1] - '0'; int b = (ch[3] - '0') * 10 + ch[4] - '0'; if (n == 24) { if (a >= 24) { ch[0] = '0'; } if (b >= 60) ch[3] = '0'; } if (n == 12) { if (a > n) { if (ch[1] != '0') ch[0] = '0'; else { ch[0] = '1'; } } if (a == 0) ch[1] = '1'; if (b >= 60) ch[3] = '0'; } printf("%s\n", ch); return 0; } ```
#include <bits/stdc++.h> using namespace std; string make(int a) { string s = ""; while (a) { s += (a % 10) + '0'; a /= 10; } while (s.size() < 2) s += "0"; swap(s[0], s[1]); return s; } int diff(string h, string m, char a, char b, char c, char d) { int ans = 0; if (h[0] != a) ans++; if (h[1] != b) ans++; if (m[0] != c) ans++; if (m[1] != d) ans++; return ans; } int main() { string s, t; int n, l = 0, r = 23, ans = 5; cin >> n >> s; if (n == 12) l = 1, r = 12; for (int i = l; i <= r; i++) { for (int j = 0; j < 60; j++) { string h = make(i), m = make(j); int curr = diff(h, m, s[0], s[1], s[3], s[4]); if (curr < ans) { ans = curr; t = ""; t += h; t += ":"; t += m; } } } cout << t; return 0; }
### Prompt Create a solution in CPP for the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string make(int a) { string s = ""; while (a) { s += (a % 10) + '0'; a /= 10; } while (s.size() < 2) s += "0"; swap(s[0], s[1]); return s; } int diff(string h, string m, char a, char b, char c, char d) { int ans = 0; if (h[0] != a) ans++; if (h[1] != b) ans++; if (m[0] != c) ans++; if (m[1] != d) ans++; return ans; } int main() { string s, t; int n, l = 0, r = 23, ans = 5; cin >> n >> s; if (n == 12) l = 1, r = 12; for (int i = l; i <= r; i++) { for (int j = 0; j < 60; j++) { string h = make(i), m = make(j); int curr = diff(h, m, s[0], s[1], s[3], s[4]); if (curr < ans) { ans = curr; t = ""; t += h; t += ":"; t += m; } } } cout << t; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, h, m; cin >> t; scanf("%d:%d", &h, &m); while (m > 59) m -= 10; if (t == 24) { while (h > 23) h -= 10; } else if (t == 12) { while (h > 12) h -= 10; if (h == 0) h++; } printf("%02d:%02d\n", h, m); }
### Prompt Construct a Cpp code solution to the problem outlined: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, h, m; cin >> t; scanf("%d:%d", &h, &m); while (m > 59) m -= 10; if (t == 24) { while (h > 23) h -= 10; } else if (t == 12) { while (h > 12) h -= 10; if (h == 0) h++; } printf("%02d:%02d\n", h, m); } ```
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } inline void rt(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) rt(x / 10), putchar(x % 10 + '0'); else putchar(x + '0'); } int sq, hh, mm; int main() { sq = rd(), hh = rd(), mm = rd(); if (sq == 12) { if (hh > 12 || !hh) if (hh % 10) hh = hh % 10; else hh = hh % 10 + 10; if (mm > 59) mm = mm % 10; } else { if (hh >= 24) hh = hh % 10; if (mm > 59) mm = mm % 10; } printf("%.2d:%.2d\n", hh, mm); }
### Prompt Please formulate a CPP solution to the following problem: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long rd() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } inline void rt(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) rt(x / 10), putchar(x % 10 + '0'); else putchar(x + '0'); } int sq, hh, mm; int main() { sq = rd(), hh = rd(), mm = rd(); if (sq == 12) { if (hh > 12 || !hh) if (hh % 10) hh = hh % 10; else hh = hh % 10 + 10; if (mm > 59) mm = mm % 10; } else { if (hh >= 24) hh = hh % 10; if (mm > 59) mm = mm % 10; } printf("%.2d:%.2d\n", hh, mm); } ```
#include <bits/stdc++.h> using namespace std; bool is_prime(long long x) { if (x == 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } bool is_palindrome(string s1) { int l = s1.length(); for (int i = 0; i < l / 2; i++) if (s1[i] != s1[l - i - 1]) return false; return true; } unsigned long long C(long long n, long long k) { if (k == 0) return 1; return (n * C(n - 1, k - 1)) / k; } long long modular_pow(long long base, long long exponent, int modulus) { long long result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } long long binaryToDec(string number) { long long result = 0, pow = 1; for (int i = number.length() - 1; i >= 0; --i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } long long binaryToDec2(string number) { long long result = 0, pow = 1; int l = number.length(); for (int i = 0; i < l; ++i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } string decimalToBin(int number) { if (number == 0) return "0"; if (number == 1) return "1"; if (number % 2 == 0) return decimalToBin(number / 2) + "0"; else return decimalToBin(number / 2) + "1"; } unsigned long long GCD(unsigned long long a, unsigned long long b) { return b == 0 ? a : GCD(b, a % b); } int cntMask(int mask) { int ret = 0; while (mask) { if (mask % 2) ret++; mask /= 2; } return ret; } int getBit(int mask, int i) { return ((mask >> i) & 1) == 1; } int setBit(int mask, int i, int value = 1) { return (value) ? mask | (1 << i) : (mask & ~(1 << i)); } unsigned long long mystoi(string s) { unsigned long long ans = 0; unsigned long long po = 1; for (int i = s.length() - 1; i >= 0; i--) { ans += (s[i] - '0') * po; po *= 10; } return ans; } string conv(int i) { string t = ""; while (i) { t += '0' + (i % 10); i /= 10; } return t; } bool hasZero(int i) { if (i == 0) return true; while (i) { if (i % 10 == 0) return true; i /= 10; } return false; } bool isSortedAc(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] > a[i + 1]) return false; return true; } bool isSortedDec(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] < a[i + 1]) return false; return true; } bool cmp(int i, int j) { return i > j; } int vo(string s) { int ret = 0; for (int i = 0; i < int(s.length()); i++) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'y') ret++; } return ret; } void solveB() { int n; int p[100 + 5]; string w[100 + 5]; cin >> n; for (int i = 0; i < int(n); i++) cin >> p[i]; getchar(); for (int i = 0; i < int(n); i++) getline(cin, w[i]); for (int i = 0; i < int(n); i++) if (vo(w[i]) != p[i]) { cout << "NO" << endl; return; } cout << "YES" << endl; return; } vector<int> hit; int n; int a[100000 + 5]; long long sums[100000 + 5] = {0}; long long getmax(int i, int j) { if (i >= j) return a[j]; int it = lower_bound(hit.begin(), hit.end(), i) - hit.begin(); if (it == n || hit[it] >= j) return sums[j] - sums[i] + a[i]; return max(getmax(i, max(0, hit[it] - 1)), getmax(min(hit[it] + 1, n - 1), j)); } int main() { int n; string s; cin >> n >> s; if (n == 12) { if (s[0] - '0' > 1 && s[1] - '0' > 0) s[0] = '0'; else if (s[0] - '0' > 1 && s[1] == '0') s[0] = '1'; else if (s[0] == '0' && s[1] == '0') s[1] = '1'; else if (s[0] == '1' && s[1] - '0' >= 2) s[1] = '2'; if (s[3] - '0' > 5) s[3] = '5'; } else { if (s[0] - '0' > 2) s[0] = '0'; else if (s[0] == '2' && s[1] - '0' >= 4) s[1] = '3'; if (s[3] - '0' > 5) s[3] = '5'; } cout << s << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. Examples Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool is_prime(long long x) { if (x == 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } bool is_palindrome(string s1) { int l = s1.length(); for (int i = 0; i < l / 2; i++) if (s1[i] != s1[l - i - 1]) return false; return true; } unsigned long long C(long long n, long long k) { if (k == 0) return 1; return (n * C(n - 1, k - 1)) / k; } long long modular_pow(long long base, long long exponent, int modulus) { long long result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } long long binaryToDec(string number) { long long result = 0, pow = 1; for (int i = number.length() - 1; i >= 0; --i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } long long binaryToDec2(string number) { long long result = 0, pow = 1; int l = number.length(); for (int i = 0; i < l; ++i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } string decimalToBin(int number) { if (number == 0) return "0"; if (number == 1) return "1"; if (number % 2 == 0) return decimalToBin(number / 2) + "0"; else return decimalToBin(number / 2) + "1"; } unsigned long long GCD(unsigned long long a, unsigned long long b) { return b == 0 ? a : GCD(b, a % b); } int cntMask(int mask) { int ret = 0; while (mask) { if (mask % 2) ret++; mask /= 2; } return ret; } int getBit(int mask, int i) { return ((mask >> i) & 1) == 1; } int setBit(int mask, int i, int value = 1) { return (value) ? mask | (1 << i) : (mask & ~(1 << i)); } unsigned long long mystoi(string s) { unsigned long long ans = 0; unsigned long long po = 1; for (int i = s.length() - 1; i >= 0; i--) { ans += (s[i] - '0') * po; po *= 10; } return ans; } string conv(int i) { string t = ""; while (i) { t += '0' + (i % 10); i /= 10; } return t; } bool hasZero(int i) { if (i == 0) return true; while (i) { if (i % 10 == 0) return true; i /= 10; } return false; } bool isSortedAc(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] > a[i + 1]) return false; return true; } bool isSortedDec(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] < a[i + 1]) return false; return true; } bool cmp(int i, int j) { return i > j; } int vo(string s) { int ret = 0; for (int i = 0; i < int(s.length()); i++) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'y') ret++; } return ret; } void solveB() { int n; int p[100 + 5]; string w[100 + 5]; cin >> n; for (int i = 0; i < int(n); i++) cin >> p[i]; getchar(); for (int i = 0; i < int(n); i++) getline(cin, w[i]); for (int i = 0; i < int(n); i++) if (vo(w[i]) != p[i]) { cout << "NO" << endl; return; } cout << "YES" << endl; return; } vector<int> hit; int n; int a[100000 + 5]; long long sums[100000 + 5] = {0}; long long getmax(int i, int j) { if (i >= j) return a[j]; int it = lower_bound(hit.begin(), hit.end(), i) - hit.begin(); if (it == n || hit[it] >= j) return sums[j] - sums[i] + a[i]; return max(getmax(i, max(0, hit[it] - 1)), getmax(min(hit[it] + 1, n - 1), j)); } int main() { int n; string s; cin >> n >> s; if (n == 12) { if (s[0] - '0' > 1 && s[1] - '0' > 0) s[0] = '0'; else if (s[0] - '0' > 1 && s[1] == '0') s[0] = '1'; else if (s[0] == '0' && s[1] == '0') s[1] = '1'; else if (s[0] == '1' && s[1] - '0' >= 2) s[1] = '2'; if (s[3] - '0' > 5) s[3] = '5'; } else { if (s[0] - '0' > 2) s[0] = '0'; else if (s[0] == '2' && s[1] - '0' >= 4) s[1] = '3'; if (s[3] - '0' > 5) s[3] = '5'; } cout << s << endl; return 0; } ```