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>
#define endl "\n"
#define finp for(int i=0; i<n; i++)
#define fknp for(int k=0; k<n; k++)
#define yeap cout<<"YES"<<endl
#define nope cout<<"NO"<<endl
#define inpintn int n; cin>>n
#define inpintarrn int arr[n]; for(int i=0; i<n; i++){cin>>arr[i];}
#define inpllarrn long long arr[n]; for(int i=0; i<n; i++){cin>>arr[i];}
#define ll long long
using namespace std;
// ll max(ll a, ll b){
// if(a<b){
// return b;
// } else {
// return a;
// }
// }
int main ()
{
int t;
cin>>t;
int j=1;
while (j<=t)
{
int n;
cin>>n;
int ar[n];
int z=n-1;
int arr[z];
int l1=0;
int l=0;
int r=0;
for (int i=0; i<n; i++){
if(i==0){
cin>>ar[i];
} else {
cin>>ar[i];
arr[i-1]=ar[i-1]-ar[i];
}
}
for (int i=0; i<z; i++){
if(arr[i]<0){
r=r-arr[i]+1;
} else if(arr[i]>0){
l1=l1+arr[i]+1;
} else if (arr[i]==0){
l1++;
r++;
}
}
int ans=r;
int max;
// cout<<r<<endl;
for (int i=1; i<z; i++){
if(arr[i-1]>0){
l=l+arr[i-1]+1;
} else if(arr[i-1]<0){
r=r+arr[i-1]-1;
} else if(arr[i-1]==0){
l++;
r--;
}
if (l<r){
max=r;
} else {
max=l;
}
if (max<ans){
ans=max;
} else {
ans=ans;
}
}
cout<<ans<<endl;
// cout<<l1<<endl;
// cout<<r<<endl;
j++;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |