Mirbek

Платформы

Dec 28th, 2021
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e5 + 5;
  6.  
  7. int n;
  8. int y[N], dp[N];
  9.  
  10. int main(){
  11.     cin >> n;
  12.  
  13.     for (int i = 1; i <= n; i++) {
  14.         cin >> y[i];
  15.     }
  16.  
  17.     dp[1] = 0;
  18.     dp[2] = abs(y[2] - y[1]);
  19.  
  20.     for (int i = 3; i <= n; i++) {
  21.         int first_value = abs(y[i] - y[i - 1]) + dp[i - 1];
  22.         int second_value = abs(y[i] - y[i - 2]) * 3 + dp[i - 2];
  23.         dp[i] = min(first_value, second_value);
  24.     }
  25.  
  26.     cout << dp[n] << endl;
  27.  
  28.     vector <int> path;
  29.  
  30.     int cur_pos = n;
  31.  
  32.     while (cur_pos >= 1) {
  33.         path.push_back(cur_pos);
  34.         if (cur_pos == 1) break;
  35.         if (cur_pos == 2) {
  36.             cur_pos = cur_pos - 1;
  37.             continue;
  38.         }
  39.         int first_value = abs(y[cur_pos] - y[cur_pos - 1]) + dp[cur_pos - 1];
  40.         int second_value = abs(y[cur_pos] - y[cur_pos - 2]) * 3 + dp[cur_pos - 2];
  41.         if (first_value < second_value) {
  42.             cur_pos = cur_pos - 1;
  43.         } else {
  44.             cur_pos = cur_pos - 2;
  45.         }
  46.     }
  47.  
  48.     reverse(path.begin(), path.end());
  49.  
  50.     cout << path.size() << endl;
  51.     for (int x : path) {
  52.         cout << x << " ";
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment