C - Linear Approximation

题意:求sigma abs(a[i]-i-b)的最小值

思路:中位数变形,a[i]-i当做新的a[i]

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int MOD=1e9+7;
ll a[N];
//struct node{
//    ll v;
//    ll d;
//}p[N];
//ll sum;
//ll ans[N];

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    cin >>n;
    for(int i=1;i<=n;i++)   cin >>a[i],a[i]-=i;
    sort(a+1,a+1+n);
    ll mid=n/2;
    ll b[4]={a[mid],a[mid+1],a[mid-1]};
    ll ans=1e18;
    for(int i=0;i<=2;i++){
        ll tb=b[i];
        ll res=0;
        for(int j=1;j<=n;j++){
            res+=abs(a[j]-tb);
        }
        ans=min(ans,res);
    }
    cout << ans << endl;

    return 0;
}