1 solutions

  • 0
    @ 2026-11-29 18:56:34

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 200005;
    long long a[N], b[N];
    
    int main() {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        sort(a + 1, a + n + 1);
        for (int i = 1; i <= n; i++) b[i] = a[i] - (i - 1);
        sort(b + 1, b + n + 1);
        long long start = b[(n + 1) / 2];   // 中位数(1-based 上中位数)
        long long ans = 0;
        for (int i = 1; i <= n; i++) {
            ans += llabs(a[i] - (start + i - 1));
        }
        printf("%lld\n", ans);
        return 0;
    }
    

    Information

    ID
    450
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By