QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378654 | #8574. Swirly Sort | ucup-team052# | WA | 26ms | 8068kb | C++23 | 2.4kb | 2024-04-06 14:02:35 | 2024-04-06 14:02:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5, M = 1e9;
int a[N], id[N];
int T, n, k;
int lc[N * 35], rc[N * 35], cnt[N * 35], rt[N], tot;
int st[N], top;
void ins(int &u, int l, int r, int x) {
if (!u) u = ++tot;
++cnt[u];
if (l == r) return;
int mid = (l + r) >> 1;
if (mid >= x) ins(lc[u], l, mid, x);
else ins(rc[u], mid + 1, r, x);
}
int merge(int u, int v, int l, int r) {
if (!u || !v) return u | v;
if (l == r) {
cnt[u] += cnt[v];
return u;
}
int mid = (l + r) >> 1;
lc[u] = merge(lc[u], lc[v], l, mid);
rc[u] = merge(rc[u], rc[v], mid + 1, r);
cnt[u] += cnt[v];
return u;
}
int query(int u, int l, int r, int need) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (cnt[lc[u]] >= need) return query(lc[u], l, mid, need);
return query(rc[u], mid + 1, r, need - cnt[lc[u]]);
}
int getM(int rt, int flag) {
int need = (cnt[rt] + 1 + flag) / 2;
return query(rt, 1, M, need);
}
int b[N];
ll calc() {
for (int i = 1; i <= tot; i++) lc[i] = rc[i] = cnt[i] = 0;
tot = top = 0;
for (int i = 1; i <= n; i++) {
rt[i] = 0;
ins(rt[i], 1, M, a[i]);
while (top && getM(rt[i], 0) < getM(rt[st[top]], 0)) {
rt[i] = merge(rt[i], rt[st[top]], 1, M);
--top;
}
st[++top] = i;
}
ll ans = 0;
for (int i = 1, j = 1; i <= top; i++) {
int len = 0;
while (j <= st[i]) {
b[++len] = a[j];
++j;
}
sort(b + 1, b + len + 1);
int mid = b[(len + 1) / 2];
for (int k = 1; k <= len; k++) ans += abs(b[k] - mid);
}
return ans;
}
bool cmp(int i, int j) {
return a[i] < a[j];
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
if (k == 1) {
printf("%lld\n", calc());
continue;
}
if (k == n) {
ll ans = 1e18;
for (int i = 1; i <= n; i++) {
ans = min(ans, calc());
rotate(a + 1, a + n, a + n + 1);
}
printf("%lld\n", ans);
continue;
}
if (k % 2 == 0) {
printf("0\n");
continue;
}
for (int i = 1; i <= n; i++) id[i] = i;
sort(id + 1, id + n + 1, cmp);
int res = 0;
for (int i = 1; i <= n; i++) {
if (id[i] != i) {
swap(id[i], id[id[i]]);
res ^= 1;
}
}
if (res == 0) {
printf("0\n");
continue;
}
int minn = 1e9;
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i++) minn = min(minn, a[i + 1] - a[i]);
printf("%d\n", minn);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8068kb
input:
4 4 1 6 4 3 7 4 2 6 4 3 7 4 3 6 4 3 7 4 4 6 4 3 7
output:
3 0 1 2
result:
ok 4 number(s): "3 0 1 2"
Test #2:
score: -100
Wrong Answer
time: 26ms
memory: 8032kb
input:
10000 4 3 524728 254456 277709 19127 15 11 360089 525234 862619 897281 336644 910706 75922 708901 754517 734744 94169 326125 746826 846063 159956 4 2 140105 792522 40264 514789 12 2 270333 888927 500833 9065 936673 982631 332435 751429 607700 840339 804685 416612 8 7 119416 689632 517277 673646 8262...
output:
23253 0 0 0 0 278544 0 0 0 0 0 0 14023 0 0 9260 0 0 51255 0 0 277173 480146 0 658 4525 0 0 0 0 0 266148 0 767231 5853 0 0 121885 0 788638 0 0 0 779611 0 5881 0 0 0 0 517074 0 5586 0 210836 454586 662851 0 781542 0 0 864957 175421 0 0 0 0 0 0 0 541010 0 0 15407 96908 0 3413333 0 321 30512 0 19677 303...
result:
wrong answer 2nd numbers differ - expected: '7691', found: '0'