QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#887016 | #2211. IOI Problem Revised | NineSuns | WA | 887ms | 96736kb | C++14 | 5.3kb | 2025-02-07 14:03:05 | 2025-02-07 14:45:34 |
Judging History
This is the latest submission verdict.
- [2025-02-07 14:35:59]
- hack成功,自动添加数据
- (/hack/1532)
- [2025-02-07 14:03:05]
- Submitted
answer
#include <bits/stdc++.h>
#define ll long long
#define pli pair <ll, int>
#define fi first
#define se second
#define pii pair <int, int>
#define pb push_back
#define int long long
using namespace std;
const int N = 1e6+5;
int n, k;
ll L, a[N], s[N];
vector <int> qs;
vector <pii> ds;
ll dis (int l, int r) {
if (l > r) return 0;
int mid = l+r>>1;
return a[mid]*(mid-l)-(s[mid-1]-s[l-1])+s[r]-s[mid]-a[mid]*(r-mid);
}
int cmp (pli x, pli y) {
if (x.fi^y.fi) return x.fi < y.fi ? 1 : -1;
if (x.se^y.se) return x.se > y.se ? 1 : -1;
return 0;
}
int cmp2 (pli x, pli y) {
if (x.fi^y.fi) return x.fi < y.fi ? 1 : -1;
if (x.se^y.se) return x.se < y.se ? 1 : -1;
return 0;
}
pli tdis (int l, int r) { return {dis(l, r), 1}; }
pli operator + (const pli & a, const pli & b) { return (pli){a.fi+b.fi, a.se+b.se}; }
pli f[N], f2[N];
ll ans = 1e18;
vector <int> td;
int ft, ed;
struct node {
int id, l, r;
}q[N];
int chk (ll c) {
f[0] = {0, 0}; q[ft = ed = 1] = (node){0, 1, n};
for (int i = 1;i <= n;i++) {
while (q[ft].r < i) ft++;
q[ft].l = i+1;
f[i] = f[q[ft].id]+tdis(q[ft].id+1, i); f[i].fi += c;
while (ft <= ed && cmp(f[i]+tdis(i+1, q[ed].l), f[q[ed].id]+tdis(q[ed].id+1, q[ed].l)) == 1) ed--;
int sl;
if (ft <= ed) {
int l = q[ed].l, r = q[ed].r+1;
while (l < r) {
int mid = l+r>>1;
if (cmp(f[i]+tdis(i+1, mid), f[q[ed].id]+tdis(q[ed].id+1, mid)) == 1) r = mid;
else l = mid+1;
}
if (l <= q[ed].r) q[ed].r = l-1; sl = l;
} else sl = i+1;
if (sl <= n) q[++ed] = (node){i, sl, n};
}
return f[n].se;
}
int ps[N];
ll g[N], ng[N];
vector <ll> tg[N];
void sol (int l, int r, int tl, int tr) {
if (l > r) return;
int mid = l+r>>1, p; ll mn = 1e18;
for (int i = tl;i <= mid && i <= tr;i++) if (g[i]+dis(i+1, mid) < mn) mn = g[i]+dis(i+1, mid), p = i;
ng[mid] = mn;
sol(l, mid-1, tl, p); sol(mid+1, r, p, tr);
}
void solve (int l, int r, vector <pii> ds) {
if (l > r) return;
int mid = l+r>>1;
vector <int> sp;
int sl, sr; sl = sr = mid;
g[mid] = 0;
for (int i = 0;i < ds.size();i++) {
sol(ds[i].fi, ds[i].se, sl, sr);
sl = ds[i].fi; sr = ds[i].se;
tg[i].clear();
for (int j = sl;j <= sr;j++) g[j] = ng[j], tg[i].push_back(g[j]);
}
int p; ll mn = 1e18;
for (int i = sl;i <= sr;i++) if (g[i]+dis(i+1, mid+n) < mn) mn = g[i]+dis(i+1, mid+n), p = i;
sp.push_back(p);
for (int i = 1l*ds.size()-2;i >= 0;i--) {
for (int j = 0;j < tg[i].size();j++) if (tg[i][j]+dis(j+ds[i].fi+1, p) == tg[i+1][p-ds[i+1].fi]) { sp.push_back(j+ds[i].fi); p = j+ds[i].fi; break; }
}
reverse(sp.begin(), sp.end());
// cout << sp.size() << " " << k << endl;
// assert(sp.size() == k-1);
if (mn < ans) {
ans = mn; td = sp; td.push_back(mid+n);
}
vector <pii> nd;
for (int i = 0;i < ds.size();i++) nd.pb({ds[i].fi, sp[i]});
solve(l, mid-1, nd);
for (int i = 0;i < ds.size();i++) nd[i] = {sp[i], ds[i].se};
solve(mid+1, r, nd);
}
inline ll mod (ll x) { return x < L ? x : x-L; }
signed main () {
cin >> n >> k >> L;
for (int i = 1;i <= n;i++) cin >> a[i], a[i+n] = a[i]+L;
// if (k == 1) {
// assert(0);
// cout << dis(1, n);// << "\n" << a[(1+n)/2];
// return 0;
// }
for (int i = 1;i <= 2*n;i++) s[i] = s[i-1]+a[i];
ll l = 0, r = 1e18;
while (l < r) {
ll mid = l+r+1>>1;
if (chk(mid) >= k) l = mid;
else r = mid-1;
}
chk(l);
f2[0] = {0, 0}; q[ft = ed = 1] = (node){0, 1, n};
for (int i = 1;i <= n;i++) {
while (q[ft].r < i) ft++;
q[ft].l = i+1;
f2[i] = f2[q[ft].id]+tdis(q[ft].id+1, i); f2[i].fi += l;
while (ft <= ed && cmp2(f2[i]+tdis(i+1, q[ed].l), f2[q[ed].id]+tdis(q[ed].id+1, q[ed].l)) == 1) ed--;
int sl;
if (ft <= ed) {
int l = q[ed].l, r = q[ed].r+1;
while (l < r) {
int mid = l+r>>1;
if (cmp2(f2[i]+tdis(i+1, mid), f2[q[ed].id]+tdis(q[ed].id+1, mid)) == 1) r = mid;
else l = mid+1;
}
if (l <= q[ed].r) q[ed].r = l-1; sl = l;
} else sl = i+1;
if (sl <= n) q[++ed] = (node){i, sl, n};
}
qs.pb(n);
for (int i = n-1;i >= 1;i--) if (f[qs.back()].fi == f[i].fi+dis(i+1, qs.back())+l && f2[i].se <= k-qs.size() && k-qs.size() <= f[i].se) qs.push_back(i);
qs.push_back(0);
reverse(qs.begin(), qs.end());
int st = -1;
for (int i = 1;i <= k;i++) qs.pb(qs[i]+n);
for (int i = 1;i < qs.size();i++) if (qs[i]-qs[i-1] <= n/k) { st = i-1; break; }
for (int i = st+1;i < st+k;i++) ds.pb({qs[i], qs[i+1]});
solve(qs[st], qs[st+1], ds);
cout << ans << "\n";
// return 0;
st = td.back()-n;
vector <ll> fd;
for (int i : td) {
fd.pb(mod(a[(st+1+i)/2])); st = i;
}
sort(fd.begin(), fd.end());
// assert(fd.size() == k); return 0;
for (ll j : fd) cout << j << " ";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 39736kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 39764kb
Test #3:
score: 0
Accepted
time: 4ms
memory: 40996kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 41408kb
Test #5:
score: 0
Accepted
time: 4ms
memory: 40872kb
Test #6:
score: 0
Accepted
time: 3ms
memory: 40348kb
Test #7:
score: 0
Accepted
time: 7ms
memory: 40860kb
Test #8:
score: 0
Accepted
time: 7ms
memory: 39804kb
Test #9:
score: 0
Accepted
time: 13ms
memory: 40440kb
Test #10:
score: 0
Accepted
time: 12ms
memory: 40252kb
Test #11:
score: 0
Accepted
time: 117ms
memory: 46020kb
Test #12:
score: 0
Accepted
time: 100ms
memory: 45692kb
Test #13:
score: 0
Accepted
time: 117ms
memory: 41564kb
Test #14:
score: 0
Accepted
time: 100ms
memory: 48676kb
Test #15:
score: 0
Accepted
time: 116ms
memory: 46984kb
Test #16:
score: 0
Accepted
time: 673ms
memory: 90016kb
Test #17:
score: 0
Accepted
time: 726ms
memory: 79104kb
Test #18:
score: 0
Accepted
time: 735ms
memory: 79684kb
Test #19:
score: 0
Accepted
time: 829ms
memory: 63956kb
Test #20:
score: 0
Accepted
time: 666ms
memory: 94028kb
Test #21:
score: 0
Accepted
time: 681ms
memory: 93568kb
Test #22:
score: 0
Accepted
time: 651ms
memory: 96480kb
Test #23:
score: 0
Accepted
time: 766ms
memory: 76568kb
Test #24:
score: 0
Accepted
time: 746ms
memory: 78096kb
Test #25:
score: 0
Accepted
time: 724ms
memory: 78268kb
Test #26:
score: 0
Accepted
time: 781ms
memory: 74856kb
Test #27:
score: 0
Accepted
time: 777ms
memory: 74100kb
Test #28:
score: 0
Accepted
time: 715ms
memory: 80440kb
Test #29:
score: 0
Accepted
time: 816ms
memory: 70828kb
Test #30:
score: 0
Accepted
time: 597ms
memory: 96736kb
Test #31:
score: 0
Accepted
time: 700ms
memory: 91540kb
Test #32:
score: 0
Accepted
time: 678ms
memory: 91708kb
Test #33:
score: 0
Accepted
time: 656ms
memory: 92744kb
Test #34:
score: 0
Accepted
time: 714ms
memory: 90288kb
Test #35:
score: 0
Accepted
time: 682ms
memory: 92896kb
Test #36:
score: 0
Accepted
time: 623ms
memory: 95836kb
Test #37:
score: 0
Accepted
time: 695ms
memory: 90764kb
Test #38:
score: 0
Accepted
time: 657ms
memory: 92328kb
Test #39:
score: 0
Accepted
time: 806ms
memory: 63340kb
Test #40:
score: 0
Accepted
time: 634ms
memory: 94732kb
Test #41:
score: 0
Accepted
time: 810ms
memory: 64512kb
Test #42:
score: 0
Accepted
time: 676ms
memory: 89656kb
Test #43:
score: 0
Accepted
time: 822ms
memory: 65696kb
Test #44:
score: 0
Accepted
time: 651ms
memory: 94548kb
Test #45:
score: 0
Accepted
time: 635ms
memory: 95876kb
Test #46:
score: 0
Accepted
time: 658ms
memory: 94876kb
Test #47:
score: 0
Accepted
time: 785ms
memory: 78660kb
Test #48:
score: 0
Accepted
time: 817ms
memory: 70244kb
Test #49:
score: 0
Accepted
time: 817ms
memory: 65308kb
Test #50:
score: 0
Accepted
time: 711ms
memory: 81512kb
Test #51:
score: 0
Accepted
time: 709ms
memory: 92624kb
Test #52:
score: 0
Accepted
time: 697ms
memory: 92780kb
Test #53:
score: 0
Accepted
time: 782ms
memory: 71284kb
Test #54:
score: 0
Accepted
time: 801ms
memory: 65484kb
Test #55:
score: 0
Accepted
time: 675ms
memory: 91580kb
Test #56:
score: 0
Accepted
time: 819ms
memory: 67416kb
Test #57:
score: 0
Accepted
time: 807ms
memory: 64348kb
Test #58:
score: 0
Accepted
time: 794ms
memory: 68940kb
Test #59:
score: 0
Accepted
time: 733ms
memory: 77204kb
Test #60:
score: 0
Accepted
time: 621ms
memory: 96716kb
Test #61:
score: 0
Accepted
time: 805ms
memory: 71868kb
Test #62:
score: 0
Accepted
time: 733ms
memory: 79692kb
Test #63:
score: 0
Accepted
time: 827ms
memory: 59800kb
Test #64:
score: 0
Accepted
time: 626ms
memory: 94488kb
Test #65:
score: 0
Accepted
time: 849ms
memory: 67724kb
Test #66:
score: 0
Accepted
time: 833ms
memory: 75984kb
Test #67:
score: 0
Accepted
time: 808ms
memory: 77688kb
Test #68:
score: 0
Accepted
time: 814ms
memory: 78676kb
Test #69:
score: 0
Accepted
time: 873ms
memory: 60288kb
Test #70:
score: 0
Accepted
time: 881ms
memory: 54924kb
Test #71:
score: 0
Accepted
time: 887ms
memory: 60152kb
Test #72:
score: 0
Accepted
time: 865ms
memory: 59568kb
Extra Test:
score: -3
Extra Test Failed : Wrong Answer on 2
time: 5ms
memory: 40696kb