Codeforces Round #592 (Div. 2) E题

给一串数,最多k次操作,每次操作可以把一个数加一或者减一
从1到n/2枚举

ll a[MAXN];

int main() {
    ll n,k,sum = 0;
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    sort(a+1,a+1+n);
    for(int i = 1; i <= n/2; i++) {
        ll x = (a[i+1] - a[i] +a[n - i + 1] - a[n - i]) * i;
        if(k >= x) k-=x;
        else{
            cout << a[n-i+1]-a[i]-k/i << endl;
            return 0;
        }
    }
    cout << 0 << endl;
    return 0;
}

你可能感兴趣的:(Codeforces Round #592 (Div. 2) E题)