This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define jizz ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
using namespace std;
ll n;
ll a[200005];
ll mi[200005] = {0};
ll nigsum, sum;
int main() {
scanf("%lld", &n);
for(ll i = 0; i < n; i++){
scanf("%lld", a+i);
if(i>0){
mi[i] = a[i]-a[i-1];
if(mi[i]>0)sum+=mi[i]+1;
else if(mi[i]<0) nigsum-=mi[i]-1;
else{
sum++;nigsum++;
}
}
}
ll ans =1e9;
ll nnigsum=0,nsum = 0;
ans = sum;
for(ll i = 1; i < n; i++){
if(mi[i]>0)nsum+=mi[i]+1;
else if(mi[i]<0) nnigsum-=mi[i]-1;
else{
nsum++;nnigsum++;
}
ans = min(ans, max(nnigsum, sum-nsum));
// printf("%lld %lld %lld\n", nnigsum, sum, nsum);
}
printf("%lld", ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%lld", &n);
| ~~~~~^~~~~~~~~~~~
Main.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%lld", a+i);
| ~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |