Advertisement
LEGEND2004

F - Platforms

Sep 7th, 2023
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int N = 1e5 + 5;
  6. const int mod = 1e18 + 7;
  7. const int INF = 1e18;
  8. int dp[N];
  9. int from[N];
  10. int a[N];
  11.  
  12. signed main()
  13. {
  14.     int n , k;
  15.     cin >> n;
  16.     for(int i = 1; i <= n; i++){
  17.         cin >> a[i];
  18.     }
  19.     dp[2] = abs(a[2] - a[1]);
  20.     from[2] = 1;
  21.     for(int i = 3; i <= n; i++){
  22.         if((dp[i - 1] + abs(a[i] - a[i - 1])) <= (dp[i - 2] + 3 * abs(a[i] - a[i - 2]))){
  23.             from[i] = i - 1;
  24.             dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]);
  25.         }else{
  26.             from[i] = i - 2;
  27.             dp[i] = dp[i - 2] + 3 * abs(a[i] - a[i - 2]);
  28.         }
  29.     }
  30.     cout << dp[n] << endl;
  31.     vector<int> v;
  32.     int x = n;
  33.     while(x){
  34.         v.push_back(x);
  35.         x = from[x];
  36.     }
  37.     reverse(v.begin() , v.end());
  38.     cout << v.size() << endl;
  39.     for(int i : v)
  40.         cout << i << " ";
  41.     cout << endl;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement