Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 5;
- int n;
- int y[N], dp[N];
- int main(){
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> y[i];
- }
- dp[1] = 0;
- dp[2] = abs(y[2] - y[1]);
- for (int i = 3; i <= n; i++) {
- int first_value = abs(y[i] - y[i - 1]) + dp[i - 1];
- int second_value = abs(y[i] - y[i - 2]) * 3 + dp[i - 2];
- dp[i] = min(first_value, second_value);
- }
- cout << dp[n] << endl;
- vector <int> path;
- int cur_pos = n;
- while (cur_pos >= 1) {
- path.push_back(cur_pos);
- if (cur_pos == 1) break;
- if (cur_pos == 2) {
- cur_pos = cur_pos - 1;
- continue;
- }
- int first_value = abs(y[cur_pos] - y[cur_pos - 1]) + dp[cur_pos - 1];
- int second_value = abs(y[cur_pos] - y[cur_pos - 2]) * 3 + dp[cur_pos - 2];
- if (first_value < second_value) {
- cur_pos = cur_pos - 1;
- } else {
- cur_pos = cur_pos - 2;
- }
- }
- reverse(path.begin(), path.end());
- cout << path.size() << endl;
- for (int x : path) {
- cout << x << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment