比赛 2025.6.7 评测结果 TAT
题目名称 Bookface 最终得分 33
用户昵称 健康铀 运行时间 7.368 s
代码语言 C++ 内存使用 4.85 MiB
提交时间 2025-06-07 11:46:45
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct B {
    vector<ll> e;
    ll m, b;
    int l, r;
};

int main() {
	
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    freopen("Bookface.in", "r", stdin);
	freopen("Bookface.out", "w", stdout);
    int z;
    cin >> z;

    while (z--) {
        int n;
        ll d;
        cin >> n >> d;
        vector<ll> c(n);
        for (int i = 0; i < n; i++) {
            cin >> c[i];
        }

        sort(c.begin(), c.end());
        vector<B> s;
        for (int i = 0; i < n; i++) {
            B t;
            t.l = i;
            t.r = i;
            ll x = c[i] - 1LL * i * d;
            t.e = {x};
            t.b = -1LL * t.l * d;
            t.m = x;
            if (t.m < t.b) {
                t.m = t.b;
            }

            while (!s.empty() && s.back().m > t.m) {
                B u = s.back();
                s.pop_back();

                t.l = min(t.l, u.l);
                t.r = max(t.r, u.r);
                t.b = -1LL * t.l * d;

                t.e.insert(t.e.end(), u.e.begin(), u.e.end());
                int sz = t.e.size();
                int mi = (sz - 1) / 2;
                nth_element(t.e.begin(), t.e.begin() + mi, t.e.end());
                ll nm = t.e[mi];
                t.m = nm;
                if (t.m < t.b) {
                    t.m = t.b;
                }
            }
            s.push_back(t);
        }

        ll tc = 0;
        for (B &b : s) {
            for (ll e : b.e) {
                tc += abs(b.m - e);
            }
        }
        cout << tc << '\n';
    }

    return 0;
}