Advertisement
Zeinab_Hamdy

Untitled

Sep 29th, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 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 ll invalid=2e18 ;
  59.  
  60. //  0 -> close
  61. //  1 -> open
  62.  
  63.  
  64. ll rec(int idx , int open  ){
  65.    
  66.     if(idx >= n-1 ) {
  67.         if(idx == n-1){
  68.             if(open)
  69.                 return vec[idx].fi;
  70.  
  71.             return invalid;
  72.         }
  73.         return invalid;
  74.     }
  75.  
  76.     ll &ret = dp[idx][open];
  77.     if(~ret)
  78.         return ret ;
  79.  
  80.     ret = invalid;
  81.  
  82.     if(open){
  83.         //  close ?
  84.         ret = min(ret , rec(idx+1 , 0 ) + vec[idx].fi );;
  85.  
  86.         //  remain open
  87.         ret = min(ret , rec(idx+1 , 1));
  88.     }
  89.     else {
  90.         ret = min(ret , rec(idx+2 , 1) -vec[idx].fi);
  91.     }
  92.    
  93.    return ret ;
  94. }
  95.  
  96. vector < int > ans ;
  97. int teams = 1 ;
  98. void build (int idx , int open){
  99.     if(idx >= n ){
  100.        
  101.         return ;
  102.     }
  103.  
  104.     ll ret = invalid ;
  105.    
  106.     //  end of interval
  107.     ll mn1 = rec(idx+1 , 0 ) + vec[idx].fi ;
  108.    
  109.     ll mn2 = rec(idx+1 , 1 );
  110.  
  111.     //  start of interval
  112.     ll mn3 = rec(idx+2 , 1 ) - vec[idx].fi;
  113.  
  114.     ret = min({mn1 , mn2 , mn3});
  115.    
  116.     if(ret == mn1){
  117.        
  118.         ans[vec[idx].se] = teams;
  119.         teams++;
  120.         build(idx+1 , 0);
  121.        
  122.     }
  123.    
  124.     else if(ret == mn2){
  125.        
  126.         build(idx+1 , 1);
  127.     }
  128.     else {
  129.         // ans[vec[idx].se ] =  -teams ;
  130.         build(idx+2 , 1);
  131.     }
  132. }
  133.  
  134. void myCode(){
  135.  
  136.  
  137. cin >> n ;
  138. vec.assign(n,{0,0});
  139. for(int i =0  , x ; i < n ; i++){
  140.     cin >> x;
  141.     vec[i] = {x , i};
  142. }
  143. ans.assign(n,0);
  144.  
  145. dp.assign(n+1 , vector < ll > ( 2 , -1));
  146. sort(all(vec));
  147.  
  148.  
  149.  
  150. cout << rec(  0 , 0 ) << " ";
  151.  
  152.  
  153. build(0,0);
  154. cout << teams << nl;
  155. cout(ans);
  156.  
  157.  
  158. }
  159.  
  160.  
  161. int main(){
  162.                                    FastCode ;
  163.                                      Zainab() ;
  164.  
  165.         int testCase=1;
  166.             // cin >> testCase ;
  167.         for(int i=1 ; i<= testCase ; i++){
  168.             myCode();
  169.         }
  170.      
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement