#include <bits/stdc++.h> #define N 1000 using namespace std; int n, tong; int h[N+ 2];…
- User
#include <bits/stdc++.h> #define N 1000 using namespace std; int n, tong; int h[N+ 2]; int a[N + 1]; int m, k, si, vi; int max(int x, int y) { return (x < y) ? y : x; } int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); freopen("ladder.inp","r",stdin); freopen("ladder.out","w",stdout); cin >> m >> n; cin >> k; fill(h, h + n + 2, 0); for (int i = 0; i < k; ++i) { cin >> vi >> si; if (h[vi] < si) h[vi] = si; } h[0] = 0; a[0] = 0; a[1] = h[1]; a[2] = max(h[1], h[2]); for (int i = 3; i <= n; ++i) { a[i] = a[i - 1] + h[i]; if (a[i] > a[i - 2] + max(h[i - 1], h[i])) a[i] = a[i - 2] + max(h[i - 1], h[i]); if (a[i] > a[i - 3] + max(max(h[i - 1], h[i]), h[i - 2])) a[i] = a[i - 3] + max(max(h[i - 1], h[i]), h[i - 2]); } tong = a[n]; cout << tong << '\n'; return 0; } dịch ý nghĩa thuật toán
- Assistant
The given code is designed to solve a problem involving a ladder and a sequence of steps that have certain heights. Here's a breakdown of the algorithm's purpose and how it works: