Advertisement
Zeinab_Hamdy

Untitled

Sep 29th, 2023
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  solution  ================================ */
  51.  
  52. vector < pair < ll , ll  > > vec;
  53.  
  54. vector < vector < ll > > dp ;
  55.  
  56.  
  57. int n ;
  58. const int invalid = 2e9 ;
  59.  
  60. //  0 -> close
  61. //  1 -> open
  62.  
  63. int cnt =0;
  64. ll rec(int idx , int open  ){
  65.    
  66.     if(idx >= n or cnt > 5) return invalid;
  67.     if(idx == n-1 ) {
  68.         if(open and (cnt < 2 or cnt > 4 )) return invalid;
  69.         return vec[idx].fi - vec[idx-cnt].fi;
  70.     }
  71.  
  72.     ll & ret = dp[idx][open];
  73.     if(~ret)
  74.         return ret ;
  75.  
  76.     ret = invalid;
  77.  
  78.     if(open){
  79.         if(cnt <= 4 ) {
  80.             cnt++;
  81.             ret = min(ret , rec(idx+1 , 1 ) );
  82.             cnt--;
  83.  
  84.  
  85.             int tempCnt = cnt ;
  86.             if(cnt >= 2 ){
  87.                 ll temp = vec[idx].fi - vec[idx-cnt].fi ;
  88.                 cnt =0 ;
  89.                 ret = min(ret , rec(idx+1 , 0 ) + temp);
  90.                 cnt = tempCnt ;
  91.             }
  92.  
  93.            
  94.         }
  95.         // else return ret = invalid ;
  96.      
  97.     }else{
  98.         cnt = 1 ;
  99.         ret = min(ret , rec(idx+1 , 1 ) );
  100.     }
  101.  
  102.     // cout << ret << nl;
  103.     return ret ;
  104.    
  105. }
  106.  
  107.  
  108.  
  109. void myCode(){
  110.  
  111.  
  112. cin >> n ;
  113. vec.assign(n,{0,0});
  114. for(int i =0  , x ; i < n ; i++){
  115.     cin >> x;
  116.     vec[i] = {x , i};
  117. }
  118.  
  119.  
  120. dp.assign(n+1 , vector < ll > ( 2 , -1));
  121. sort(all(vec));
  122.  
  123.  
  124.  
  125. cout << rec(0 , 0 ) << " ";
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133.  
  134. int main(){
  135.                                    FastCode ;
  136.                                      Zainab() ;
  137.  
  138.         int testCase=1;
  139.             // cin >> testCase ;
  140.         for(int i=1 ; i<= testCase ; i++){
  141.             myCode();
  142.         }
  143.      
  144.  
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement