Created
July 10, 2018 04:52
-
-
Save ateruimashin/34231996d4265cba875bd19d3cb31f24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <math.h> | |
using namespace std; | |
using ll = long long; | |
int main(int argc, char const *argv[]) { | |
int n; | |
cin>>n; | |
ll a[n]; | |
for(int i=0;i<n;i++){ | |
ll tmp; | |
cin>>tmp; | |
a[i]=tmp-i; | |
} | |
ll med1=0,med2=0,median=0,ans=0; | |
sort(a,a+n); | |
if(n%2==0){ | |
med1=a[n/2-1]; | |
med2=a[n/2]; | |
median=(med1+med2)/2; | |
}else{ | |
median=a[n/2]; | |
} | |
for(int i=0;i<n;i++){ | |
ans+=abs(a[i]-median); | |
} | |
cout<<ans<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment