Submission #882901

#TimeUsernameProblemLanguageResultExecution timeMemory
882901hasan2006Growing Vegetables is Fun 4 (JOI21_ho_t1)C++17
0 / 100
1 ms6492 KiB
#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 5e5 + 9 , mod = 1e9 + 7;
ll   a[N] = {}, b[N] , c[N] , d[N] , dp[N] , ans[N];

void solve()
{
    ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
    cin>>n;
    for(i = 1; i <= n; i++){
        cin>>a[i];
    }
    b[1] = c[n] = 0;
    for(i = 1;i  <= n; i++){
        mx = max(mx , a[i]);
        b[i] =  mx - a[i];
        mx++;
    }
    mx = 0;
    for(i = n;i >=  1; i--){
        mx = max(mx , a[i]);
        c[i] = mx - a[i];
        mx++;
    }
    for(i = 1; i <= n; i++)
        s += max(c[i] - c[i - 1] , 0ll);
    for(i = 1; i <= n; i++){
        x = max(c[i] , b[i]);
        //if(i > 1){
            s -= (max(c[i] - b[i - 1] , 0ll));
            s += max(x  - b[i - 1] , 0ll);
        //}
        if(i != n){
            s -= max(c[i + 1] - c[i] , 0ll);
            s += max(c[i + 1] - x , 0ll);
        }
        y = s;
        k = 0;
        if(i > 1)
            k = b[i - 2];
        //if(i == 6)
        //    cout<<y<<" "<<c[i + 1]<<" "<<x<<" "<<b[i - 1]<<" "<<k<<"\n";
        if(b[i - 1] >= x && c[i + 1] >= x && (b[i - 1] - x) >= 0)
            y -= max(min(c[i + 1] - x , (b[i - 1] - x)) , 0ll);
        mn = min(mn , y);
        //cout<<y<<"\n";
        //cout<<s<<" ";
        //if(i > 1){
            s -= max(x - b[i - 1] , 0ll);
            s += max(b[i] - b[i - 1] , 0ll);
        //}
        if(i != n){
            s -= max(c[i + 1] - x , 0ll);
            s += max(c[i + 1] - b[i] , 0ll);
        }
      //  cout<<s<<" ";
    }
    cout<<mn<<"\n";
}

int main(){
int t = 1;
//cin>>t;
while(t--)
     {
     solve();
     }
}
// Author : حسن

Compilation message (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:26:8: warning: unused variable 'q' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |        ^
Main.cpp:26:16: warning: unused variable 'j' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                ^
Main.cpp:26:20: warning: unused variable 'm' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                    ^
Main.cpp:26:26: warning: unused variable 'z' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                          ^
Main.cpp:26:37: warning: unused variable 'f' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                     ^
Main.cpp:26:40: warning: unused variable 'l' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                        ^
Main.cpp:26:44: warning: unused variable 'r' [-Wunused-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                            ^
Main.cpp:26:48: warning: variable 'k' set but not used [-Wunused-but-set-variable]
   26 |     ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...