QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#379820 | #8574. Swirly Sort | ucup-team191# | WA | 6ms | 3868kb | C++23 | 1.9kb | 2024-04-06 19:18:24 | 2024-04-06 19:18:25 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)
const int N=300010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;
int t, n, k;
int fen[N];
vl a;
ll calc (vl &v) {
multiset <ll> st;
ll sol = 0;
for (auto x : v) {
if (st.empty() || x >= *st.rbegin()) {
st.insert(x);
} else {
ll mx = *st.rbegin();
st.insert(x);
st.insert(x);
st.erase(st.find(mx));
sol += mx - x;
}
}
return sol;
}
void okreni (vl &v) {
ll prvi = v[0];
for (int i = 1; i < (int)v.size(); i++) {
v[i - 1] = v[i];
}
v[(int)v.size() - 1] = prvi;
}
void ubaci (int x, int k) {
for (; x < N; x += x&-x) fen[x] += k;
}
int upit (int x) {
int res = 0;
for (; x > 0; x -= x&-x) res += fen[x];
return res;
}
int parnost (vl &v) {
vector < pair <ll, int> > r;
for (int i = 0; i < n; i++) {
r.push_back({v[i], i + 1});
}
sort(r.begin(), r.end());
ll cnt = 0;
for (int i = 0; i <= n; i++) {
fen[i] = 0;
}
for (int i = 0; i < n; i++) {
int val = r[i].second;
cnt += upit(val);
ubaci(val, 1);
}
return cnt % 2;
}
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (k == 1) {
cout << calc(a) << '\n';
} else if (k == n) {
ll sol = LLINF;
for (int i = 0; i < n; i++) {
sol = min(sol, calc(a));
okreni(a);
}
cout << sol << '\n';
} else if (k % 2 == 0) {
cout << 0 << '\n';
} else if (parnost(a) == 0) {
cout << 0 << '\n';
} else {
ll sol = LLINF;
sort(a.begin(), a.end());
for (int i = 1; i < n; i++) {
sol = min(sol, a[i] - a[i - 1]);
}
cout << sol << '\n';
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3868kb
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: 6ms
memory: 3540kb
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 15986 278544 0 0 0 0 0 2022 0 0 0 9260 0 0 51255 0 0 277173 480146 0 0 4525 25495 0 0 0 0 266148 0 767231 0 0 0 121885 0 788638 0 0 0 779611 0 0 0 0 0 0 517074 0 0 0 210836 454586 662851 0 781542 0 0 864957 175421 0 0 0 0 0 0 0 541010 0 0 0 96908 0 3413333 0 321 0 0 19677 30305 3095770 1...
result:
wrong answer 2nd numbers differ - expected: '7691', found: '0'