Alex_tz307

IOIT rollercoaster2 - 0p

Dec 15th, 2020 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define ABS(x) ((x) >= 0 ? (x) : -(x))
  4.  
  5. using namespace std;
  6.  
  7. const int INF = 1e16L;
  8.  
  9. void min_self(int &a, int b) {
  10.     a = min(a, b);
  11. }
  12.  
  13. int32_t main() {
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(nullptr);
  16.     cout.tie(nullptr);
  17.     int N;
  18.     cin >> N;
  19.     vector<int> a(N + 1);
  20.     for(int i = 1; i <= N; ++i)
  21.         cin >> a[i];
  22.     vector<vector<int>> dp(N + 1, vector<int>(8192, INF));
  23.     for(int i = 0; i < 6000; ++i) {
  24.         dp[1][i] = ABS(a[1] - i) * ABS(a[1] - i);
  25.         if(i > 0)
  26.             min_self(dp[1][i], dp[1][i - 1]);
  27.     }
  28.     for(int i = 2; i <= N; ++i) {
  29.         if(!(i & 1))
  30.             for(int j = 5999; j > 0; --j) {
  31.                 dp[i][j] = dp[i - 1][j - 1] + ABS(j - a[i]) * ABS(j - a[i]);
  32.                 min_self(dp[i][j], dp[i][j + 1]);
  33.             }
  34.         else
  35.             for(int j = 1; j < 6000; ++j) {
  36.                 dp[i][j] = dp[i - 1][j + 1] + ABS(j - a[i]) * ABS(j - a[i]);
  37.                 if(j > 1)
  38.                     min_self(dp[i][j], dp[i][j - 1]);
  39.             }
  40.     }
  41.     int ans = INF;
  42.     for(int j = 1; j < 6000; ++j)
  43.         min_self(ans, dp[N][j]);
  44.     dp = vector<vector<int>>(N + 1, vector<int>(8192, INF));
  45.     for(int i = 5999; i > 0; --i) {
  46.         dp[1][i] = ABS(a[1] - i) * ABS(a[1] - i);
  47.         min_self(dp[1][i], dp[1][i + 1]);
  48.     }
  49.     for(int i = 2; i <= N; ++i) {
  50.         if(i & 1)
  51.             for(int j = 5999; j > 0; --j) {
  52.                 dp[i][j] = dp[i - 1][j - 1] + ABS(j - a[i]) * ABS(j - a[i]);
  53.                 min_self(dp[i][j], dp[i][j + 1]);
  54.             }
  55.         else
  56.             for(int j = 1; j < 6000; ++j) {
  57.                 dp[i][j] = dp[i - 1][j + 1] + ABS(j - a[i]) * ABS(j - a[i]);
  58.                 if(j > 1)
  59.                     min_self(dp[i][j], dp[i][j - 1]);
  60.             }
  61.     }
  62.     for(int j = 1; j < 6000; ++j)
  63.         min_self(ans, dp[N][j]);
  64.     cout << ans;
  65. }
  66.  
Add Comment
Please, Sign In to add comment