SHARE
TWEET

Untitled

a guest Feb 27th, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <math.h>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <deque>
  13. #include <stack>
  14. #include <iostream>
  15.  
  16. #define INF 1e18 + 9
  17.  
  18.  
  19.  
  20. typedef long long ll;
  21.  
  22.  
  23. using namespace std;
  24.  
  25. ll a[100005], dp[200005][3];
  26.    
  27. int n;
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33.     ios_base::sync_with_stdio(0);
  34.     cin.tie(0);
  35.     cout.tie(0);
  36.  
  37.     cin >> n;
  38.     for(int i = 0; i < n; i++)
  39.         cin >> a[i];
  40.  
  41.     dp[0][1] = dp[0][0] = dp[0][2] = 0;
  42.  
  43.  
  44.     for (int i = 1; i < n; i++)
  45.     {
  46.         ll mn = min(dp[i - 1][0], min(dp[i - 1][1], dp[i - 1][2]));
  47.         dp[i][0] = mn + abs(a[i] - a[i - 1]) * abs(a[i] - a[i - 1]);
  48.  
  49.  
  50.         if (i >= 2)
  51.         {
  52.             ll mn2 = min(dp[i - 2][0], min(dp[i - 2][1], dp[i - 2][2]) );
  53.             dp[i][1] = mn2 + abs(a[i - 2] - a[i]) * abs(a[i - 2] - a[i]) * 3;
  54.         }
  55.         else
  56.             dp[i][1] = INF;
  57.  
  58.         if (i >= 3)
  59.         {
  60.             dp[i][2] = mn + abs(a[i - 1] - a[i - 2]) * abs(a[i - 1] - a[i - 2]) + abs(a[i - 2] - a[i]) * abs(a[i - 2] - a[i]) * 3;
  61.         }
  62.         else
  63.             dp[i][2] = INF;
  64.  
  65.     }
  66.  
  67.  
  68.        
  69.     cout << min(dp[n - 1][0], min(dp[n - 1][1], dp[n - 1][2]));
  70.  
  71.  
  72.  
  73.  
  74.     return 0;
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top