QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378579 | #8574. Swirly Sort | ucup-team1004# | WA | 15ms | 10036kb | C++14 | 1.9kb | 2024-04-06 13:38:35 | 2024-04-06 13:38:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(a) (a).begin(),(a).end()
#ifdef DEBUG
template<class T>
ostream& operator << (ostream &out,vector<T> a){
out<<'[';
for(T x:a)out<<x<<',';
return out<<']';
}
template<class T>
vector<T> ary(T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
using LL=__int128;
const int N=3e5+10;
const ll INF=1e18;
int T,n,k,a[N];
ll solve(int l=1,int r=n,int L=0,int R=1e9){
if(l>r)return 0;
if(L==R){
ll sum=0;
for(int i=l;i<=r;i++)sum+=abs(a[i]-L);
return sum;
}
int mid=(L+R)>>1;
static ll f[N],g[N];
f[l-1]=g[r+1]=0;
for(int i=l;i<=r;i++)f[i]=f[i-1]+abs(a[i]-mid);
for(int i=r;i>=l;i--)g[i]=g[i+1]+abs(a[i]-mid-1);
int cur=-1;
ll mn=INF;
for(int i=l;i<=r+1;i++){
ll val=f[i-1]+g[i];
if(val<mn)mn=val,cur=i;
}
return solve(l,cur-1,L,mid)+solve(cur,r,mid+1,R);
}
int cur[N],c[N];
void add(int x,int y){
for(;x;x^=x&-x)c[x]+=y;
}
int get(int x,int y=0){
for(;x<=n;x+=x&-x)y+=c[x];
return y;
}
void get(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
if(k==1){
printf("%lld\n",solve());
}else if(k==n){
ll ans=INF;
for(int i=1;i<=n;i++){
ans=min(ans,solve());
rotate(a+1,a+2,a+1+n);
}
printf("%lld\n",ans);
}else if(k==2)puts("0");
else{
iota(cur,cur+1+n,0);
sort(cur+1,cur+1+n,[&](int x,int y){
return a[x]<a[y];
});
fill(c+1,c+1+n,0);
ll ans=0;
for(int i=1;i<=n;i++){
ans+=get(cur[i]);
add(cur[i],1);
}
if(ans&1){
ll mn=INF;
for(int i=1;i<n;i++){
mn=min(mn,(ll)a[cur[i+1]]-a[cur[i]]);
}
printf("%lld\n",mn);
}else puts("0");
}
}
int main(){
for(scanf("%d",&T);T--;)get();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8060kb
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: 15ms
memory: 10036kb
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 7691 0 0 15986 278544 0 0 0 0 0 2022 0 0 0 9260 0 0 51255 0 0 277173 480146 3043 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 0 8153 210836 454586 662851 0 781542 0 0 864957 175421 0 0 0 0 710 0 0 541010 0 0 15407 0 0 3413333 0 0 0 0 19677 30...
result:
wrong answer 24th numbers differ - expected: '0', found: '3043'