QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103263#2211. IOI Problem RevisedUnique_HanpiAC ✓694ms28356kbC++144.7kb2023-05-04 21:42:122023-05-04 22:47:35

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-04 22:47:35]
  • 评测
  • 测评结果:AC
  • 用时:694ms
  • 内存:28356kb
  • [2023-05-04 21:42:12]
  • 提交

answer

//路径交错
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define eb emplace_back
#define pb push_back
#define mp make_pair
using namespace std;

typedef long long ll;
const int N = 4e5+5;
const int Mod = 998244353;

int n, k;
int p[N * 2];
ll L, a[N], s[N], mn, f[N];

vector<int> x;
vector<ll> ans;

inline ll w(int l, int r) {
    int m1 = (l + r) >> 1;
    int m2 = (l + r - 1) >> 1;
    return s[r] - s[m1] - s[m2] + s[l - 1];
}

namespace wqs {
    int head, tail;
    ll g[N], c[2][N];

    struct info {
        int l, r, p;
    } Q[N];
    
    inline bool cmp(int i, int j, int k, bool t) {
        ll fi = g[i] + w(i + 1, k), fj = g[j] + w(j + 1, k);
        return fi < fj || (!t && fi == fj);
    }

    inline int chk(ll v, bool t) {
        Q[head = tail = 1] = (info) { 1, n, 0 };
        for (int i = 1; i <= n; i++) {
            g[i] = g[Q[head].p] + v + w(Q[head].p + 1, i);
            c[t][i] = c[t][Q[head].p] + 1;
            if (Q[head].r < i + 1) head++;
            Q[head].l = i + 1;
            while (head <= tail && !cmp(Q[tail].p, i, Q[tail].l, t)) tail--;
            if (head > tail) {
                Q[++tail] = (info) { i + 1, n, i };
            } else {
                int L = Q[tail].l + 1, R = Q[tail].r, ans = R + 1;
                while (L <= R) {
                    int mid = (L + R) >> 1;
                    if (!cmp(Q[tail].p, i, mid, t)) ans = mid, R = mid - 1;
                    else L = mid + 1;
                }
                Q[tail].r = ans - 1;
                if (ans <= n) Q[++tail] = (info) { ans, n, i };
            }
        }
        return c[0][n];
    }

    inline void work() {
        ll L = 0, R = 1e18, ans = R + 1;
        while (L <= R) {
            ll mid = (L + R) >> 1;
            if (chk(mid, 0) <= k) ans = mid, R = mid - 1;
            else L = mid + 1;
        }
        chk(ans, 0);
        chk(ans, 1);
        mn = g[n] - ans * k;

        //构造方案
        int cur = 0, lst = n;
        for (int i = n - 1; ~i; i--)
            if (g[i] + ans + w(i + 1, lst) == g[lst] && cur + 1 + c[0][i] <= k && k <= cur + 1 + c[1][i])
                cur++, x.pb(lst = i);
        reverse(x.begin(), x.end());
        x.pb(n);
    }
}

void solve(int l, int r, int L, int R, int d) {
    if (l > r) return;
    int mid = (l + r) >> 1;
    ll g = 1e18;
    for (int i = L; i <= min(mid, R); i++) {
        ll tmp = f[i] + w(i + 1, mid);
        if (tmp <= g) g = tmp, p[mid + d] = i;
    }
    solve(mid + 1, r, p[mid + d], R, d);
    solve(l, mid - 1, L, p[mid + d], d);
    f[mid] = g;
}

void solve(vector<int> L, vector<int> R) {
    if (L[0] > R[0]) return;

    int mid = (L[0] + R[0]) >> 1;

    for (int i = L[1]; i <= R[1]; i++) f[i] = w(mid + 1, i), p[i + 1] = mid;
    for (int i = 2; i < k; i++) solve(L[i], R[i], L[i - 1], R[i - 1], i);
    solve(mid + n, mid + n, L[k - 1], R[k - 1], k);

    vector<int> md(0);
    int cur = mid + n, ck = k;
    while (ck--) md.pb(cur = p[cur + ck + 1]);
    reverse(md.begin(), md.end());
    md.pb(mid + n);

    // puts("-----------");
    // printf("%lld\n", f[mid + n]);
    // for (int i : md) printf("%d ", i);
    // puts("\n------------");

    if (f[mid + n] < mn) {
        mn = f[mid + n];
        x = md;
    }

    md[0]--;
    solve(L, md);
    md[0] += 2;
    solve(md, R);
}

int main() {
    scanf("%d%d%lld", &n, &k, &L);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), s[i] = s[i - 1] + a[i];
    for (int i = n + 1; i <= 2 * n; i++) a[i] = a[i - n] + L, s[i] = s[i - 1] + a[i];

    if (k == 1) {
        int ans = 1;
        for (int i = 2; i <= n; i++) if (w(i, i + n - 1) < w(ans, ans + n - 1)) ans = i;
        printf("%lld\n", w(ans, ans + n - 1));
        printf("%lld", a[ans + (n - 1) / 2] % L);
        return 0;
    }

    wqs::work();

    // printf("%lld\n", mn);
    // for (int i : x) printf("%d ", i); puts("");

    int id = 1;
    for (int i = 1; i <= k; i++)
        if (x[i] - x[i - 1] < x[id] - x[id - 1]) id = i;
    
    vector<int> tl(k + 1), tr(k + 1);
    tl[0] = x[id - 1] + (id == 1);
    tr[0] = x[id];
    for (int i = id + 1; i <= k; i++) tl[i - id] = x[i - 1], tr[i - id] = x[i];
    for (int i = 1; i < id; i++) tl[k - id + i] = x[i - 1] + n, tr[k - id + i] = x[i] + n;

    // for (int i : tl) printf("%d ", i); puts("");
    // for (int i : tr) printf("%d ", i); puts("");

    solve(tl, tr);
    
    printf("%lld\n", mn);
    for (int i = 1; i <= k; i++) ans.pb(a[(x[i] + x[i - 1] + 1) >> 1] % L);

    sort(ans.begin(), ans.end());

    for (ll i : ans) printf("%lld ", i);
    return 0;
}
/*
5 3 10
0 1 3 7 9 
*/

这程序好像有点Bug,我给组数据试试?

Details

Test #1:

score: 100
Accepted
time: 4ms
memory: 9800kb

Test #2:

score: 0
Accepted
time: 1ms
memory: 9792kb

Test #3:

score: 0
Accepted
time: 2ms
memory: 9772kb

Test #4:

score: 0
Accepted
time: 1ms
memory: 9796kb

Test #5:

score: 0
Accepted
time: 3ms
memory: 9800kb

Test #6:

score: 0
Accepted
time: 2ms
memory: 9808kb

Test #7:

score: 0
Accepted
time: 3ms
memory: 9852kb

Test #8:

score: 0
Accepted
time: 2ms
memory: 9864kb

Test #9:

score: 0
Accepted
time: 5ms
memory: 9932kb

Test #10:

score: 0
Accepted
time: 13ms
memory: 9976kb

Test #11:

score: 0
Accepted
time: 99ms
memory: 13072kb

Test #12:

score: 0
Accepted
time: 84ms
memory: 11544kb

Test #13:

score: 0
Accepted
time: 94ms
memory: 15024kb

Test #14:

score: 0
Accepted
time: 73ms
memory: 13264kb

Test #15:

score: 0
Accepted
time: 89ms
memory: 13024kb

Test #16:

score: 0
Accepted
time: 510ms
memory: 24264kb

Test #17:

score: 0
Accepted
time: 563ms
memory: 22532kb

Test #18:

score: 0
Accepted
time: 569ms
memory: 22988kb

Test #19:

score: 0
Accepted
time: 627ms
memory: 20332kb

Test #20:

score: 0
Accepted
time: 517ms
memory: 24456kb

Test #21:

score: 0
Accepted
time: 510ms
memory: 26488kb

Test #22:

score: 0
Accepted
time: 511ms
memory: 23544kb

Test #23:

score: 0
Accepted
time: 599ms
memory: 21912kb

Test #24:

score: 0
Accepted
time: 583ms
memory: 25088kb

Test #25:

score: 0
Accepted
time: 551ms
memory: 26388kb

Test #26:

score: 0
Accepted
time: 603ms
memory: 23480kb

Test #27:

score: 0
Accepted
time: 600ms
memory: 23224kb

Test #28:

score: 0
Accepted
time: 562ms
memory: 26228kb

Test #29:

score: 0
Accepted
time: 612ms
memory: 21020kb

Test #30:

score: 0
Accepted
time: 463ms
memory: 25852kb

Test #31:

score: 0
Accepted
time: 537ms
memory: 25208kb

Test #32:

score: 0
Accepted
time: 520ms
memory: 24252kb

Test #33:

score: 0
Accepted
time: 496ms
memory: 23464kb

Test #34:

score: 0
Accepted
time: 560ms
memory: 25172kb

Test #35:

score: 0
Accepted
time: 531ms
memory: 24208kb

Test #36:

score: 0
Accepted
time: 480ms
memory: 27352kb

Test #37:

score: 0
Accepted
time: 532ms
memory: 24068kb

Test #38:

score: 0
Accepted
time: 516ms
memory: 28356kb

Test #39:

score: 0
Accepted
time: 652ms
memory: 20624kb

Test #40:

score: 0
Accepted
time: 484ms
memory: 27136kb

Test #41:

score: 0
Accepted
time: 662ms
memory: 18356kb

Test #42:

score: 0
Accepted
time: 538ms
memory: 27932kb

Test #43:

score: 0
Accepted
time: 639ms
memory: 21528kb

Test #44:

score: 0
Accepted
time: 502ms
memory: 25100kb

Test #45:

score: 0
Accepted
time: 482ms
memory: 26996kb

Test #46:

score: 0
Accepted
time: 503ms
memory: 25420kb

Test #47:

score: 0
Accepted
time: 600ms
memory: 20660kb

Test #48:

score: 0
Accepted
time: 611ms
memory: 21188kb

Test #49:

score: 0
Accepted
time: 630ms
memory: 23832kb

Test #50:

score: 0
Accepted
time: 559ms
memory: 24928kb

Test #51:

score: 0
Accepted
time: 543ms
memory: 26496kb

Test #52:

score: 0
Accepted
time: 554ms
memory: 24696kb

Test #53:

score: 0
Accepted
time: 617ms
memory: 22108kb

Test #54:

score: 0
Accepted
time: 607ms
memory: 21896kb

Test #55:

score: 0
Accepted
time: 524ms
memory: 26172kb

Test #56:

score: 0
Accepted
time: 623ms
memory: 24648kb

Test #57:

score: 0
Accepted
time: 639ms
memory: 23172kb

Test #58:

score: 0
Accepted
time: 663ms
memory: 20708kb

Test #59:

score: 0
Accepted
time: 609ms
memory: 25932kb

Test #60:

score: 0
Accepted
time: 477ms
memory: 27060kb

Test #61:

score: 0
Accepted
time: 593ms
memory: 21788kb

Test #62:

score: 0
Accepted
time: 603ms
memory: 25008kb

Test #63:

score: 0
Accepted
time: 642ms
memory: 21308kb

Test #64:

score: 0
Accepted
time: 487ms
memory: 25448kb

Test #65:

score: 0
Accepted
time: 657ms
memory: 21216kb

Test #66:

score: 0
Accepted
time: 669ms
memory: 22636kb

Test #67:

score: 0
Accepted
time: 627ms
memory: 23796kb

Test #68:

score: 0
Accepted
time: 647ms
memory: 24292kb

Test #69:

score: 0
Accepted
time: 694ms
memory: 17944kb

Test #70:

score: 0
Accepted
time: 694ms
memory: 19356kb

Test #71:

score: 0
Accepted
time: 690ms
memory: 20612kb

Test #72:

score: 0
Accepted
time: 679ms
memory: 17944kb

Extra Test:

score: 0
Extra Test Passed