Submission #393109

#TimeUsernameProblemLanguageResultExecution timeMemory
393109ahmetGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms460 KiB
#include <bits/stdc++.h> using namespace std; #define zaman cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds " #define rep(i,n) for(long long (i)=0;(i)<(n);++(i)) #define ref(i,a,b) for (long long (i)=(a); (i)<=(b); ++(i)) #define endl '\n' #define st first #define nd second #define ll long long #define pb push_back #define pii pair<ll,ll> #define mp make_pair const int mx=2e5+6; ll a[mx]; ll l[mx],r[mx],b[mx],c[mx],ka[mx],kb[mx]; ll ans=1e18; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n;cin >> n; ref(i,1,n){ cin >> a[i]; } ll val=a[1]+1; ll res=0,need; ka[1]=a[1]; for(int i=2;i<=n;++i){ if(a[i]>val){ ka[i]=val; val=a[i]+1; l[i]=l[i-1]; b[i]=0; continue; } need=val-a[i]; if(need<=b[i-1]){ l[i]=l[i-1]; } else{ l[i]=l[i-1]+need-b[i-1]; } b[i]=need; ka[i]=val; ++val; } val=a[n]+1; res=0; kb[n]=a[n]; for(int i=n-1;i>=1;--i){ if(a[i]>val){ kb[i]=val; val=a[i]+1; r[i]=r[i+1]; c[i]=0; continue; } need=val-a[i]; if(need<=c[i+1]){ r[i]=r[i+1]; } else{ r[i]=r[i+1]+need-c[i+1]; } c[i]=need; kb[i]=val; ++val; } ans=min(r[1],l[n]); for(int i=2;i<=n-1;++i){ ll left=ka[i-1]; ll right=kb[i+1]; ll kel=max(left,right)+1; need=kel-a[i]-max(c[i+1],b[i-1]); if(need<0)need=0; ll efl=min(c[i+1],b[i-1]); //if(i==4)cout <<i<<" "<<need<<" "<<efl<<" " << l[i-1]+r[i+1]+need-efl<< endl; ans=min(ans,l[i-1]+r[i+1]+need-efl); } cout << ans << endl; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:5:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    5 | #define ref(i,a,b) for (long long (i)=(a); (i)<=(b); ++(i))
      |                                   ^
Main.cpp:20:2: note: in expansion of macro 'ref'
   20 |  ref(i,1,n){
      |  ^~~
Main.cpp:24:5: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   24 |  ll res=0,need;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...