Advertisement
tepyotin2

Clear the Multiset

Jun 23rd, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. int n;
  8. vector<ll> arr;
  9.  
  10. ll operation(int l, int r){
  11.     //cout << "l: " << l << ", r: " << r << '\n';
  12.     if(l>r) return 0;
  13.     ll mn = LLONG_MAX;
  14.     for(int i=l; i<=r; i++){
  15.         //cout << "i: " << i <<  ", arr[i]: " << arr[i] << '\n';
  16.         mn = min(mn, arr[i]);
  17.     }
  18.     //return 0;
  19.     //int start = l;
  20.     for(int i=l; i<=r; i++){
  21.         arr[i]-=mn;
  22.         //if(arr[i]>0){
  23.             //start = i;
  24.         //}
  25.     }
  26.     int start = l;
  27.     ll ops = mn;
  28.     //!!! ops must start equal to mn or else value will always just be 0
  29.     for(int i=l; i<=r; i++){
  30.         //cout << "i: " << i << ", arr[i]: " << arr[i] << '\n';
  31.         //values after erase and if equal to zero then seperate array to go check the seperated part
  32.         if(arr[i] == 0){
  33.             ops+=operation(start, i-1);
  34.             start = i+1;
  35.         }
  36.     }
  37.     ops+=operation(start, r);
  38.     return min((ll)(r-l)+1, ops);
  39.     //at the end compare between operation 1 and operation 2 which operation 2 is just (r-l)+1 because individual
  40. }
  41.  
  42. int main(){
  43.     //freopen("clearmultiset.in", "r", stdin);
  44.    
  45.     cin >> n;
  46.     //vector<int> arr;
  47.     arr.resize(n+1);
  48.     for(int i=1; i<=n; i++){
  49.         cin >> arr[i];
  50.     }
  51.     cout << operation(1, n) << '\n';
  52.     //cout << "HI" << '\n';
  53.    
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement