QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#596218 | #5143. Quick Sort | louhao088# | TL | 35ms | 8020kb | C++23 | 1.5kb | 2024-09-28 15:21:12 | 2024-09-28 15:21:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+5;
int n,m,a[maxn],T,pos[maxn],sum,b[maxn];
bool cmp(int x,int y){return x>y;}
void solve(int l,int r){
if(l>=r||l<=0||r>n)return ;
int x=a[(l+r)/2];
int p;
//cout<<l<<" "<<r<<" "<<x<<endl;
if(x-l+1<r-x){
int cnt=0;
for(int i=l;i<=x;i++){
b[++cnt]=pos[i];
}
sort(b+1,b+cnt+1);
int L=l-1,R=r+1,id=l;
//cout<<L<<" "<<R<<endl;
while(L<=R){
L++;
while(a[L]<x&&L<=r)L++;
R=b[cnt];
if(!cnt){p=L-1;break;}
if(L>=R){p=R;break;}
swap(a[L],a[R]);
pos[a[L]]=L,pos[a[R]]=R;
sum++;cnt--;
}
//cout<<p<<endl;
}
else{
int cnt=0;
for(int i=x;i<=r;i++){
b[++cnt]=pos[i];
}
sort(b+1,b+cnt+1,cmp);
int L=l-1,R=r+1,id=l;
//cout<<L<<" "<<R<<endl;
while(L<=R){
L=b[cnt];
R--;
while(a[R]>x&&R>=l)R--;
if(!cnt){p=R;break;}
if(L>=R){p=R;break;}
swap(a[L],a[R]);
pos[a[L]]=L,pos[a[R]]=R;
cnt--;sum++;
}//cout<<p<<endl;
}
solve(l,p),solve(p+1,r);
}
void SSolve(){
scanf("%d",&n);sum=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i;
solve(1,n);
printf("%d\n",sum);
}
signed main(){
scanf("%d",&T);
while(T--){
SSolve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7940kb
input:
3 3 3 2 1 5 2 4 5 3 1 10 7 2 4 6 1 9 10 8 5 3
output:
1 4 7
result:
ok 3 number(s): "1 4 7"
Test #2:
score: 0
Accepted
time: 35ms
memory: 8020kb
input:
100000 4 3 4 2 1 5 5 4 1 3 2 4 3 1 4 2 4 4 2 1 3 4 1 3 2 4 4 4 2 3 1 4 3 2 1 4 5 1 2 3 4 5 5 5 2 3 1 4 5 1 3 5 4 2 5 4 3 2 1 5 5 3 4 2 1 5 5 5 4 3 2 1 4 3 4 2 1 5 4 2 5 1 3 5 4 1 5 2 3 4 3 4 1 2 4 2 1 3 4 5 4 3 2 5 1 4 4 2 1 3 5 3 1 5 2 4 4 4 1 2 3 5 1 5 2 4 3 5 4 1 3 5 2 5 4 2 3 5 1 5 1 2 3 4 5 5 4...
output:
3 4 3 2 1 1 1 0 2 2 2 3 2 3 2 3 2 1 3 2 4 3 2 3 2 0 4 2 4 1 3 2 3 2 2 1 3 1 1 2 4 3 2 3 1 1 1 3 3 3 4 4 2 3 3 2 3 3 3 2 1 1 2 3 1 3 1 1 3 4 2 2 4 1 1 3 2 2 2 2 1 3 4 4 3 4 2 2 1 3 2 3 2 3 3 2 1 4 2 3 4 1 2 2 3 2 2 3 2 2 2 2 4 1 2 3 2 2 2 2 3 2 3 4 3 2 3 4 2 4 1 3 2 3 4 3 3 4 1 2 4 3 2 4 2 3 3 1 2 2 ...
result:
ok 100000 numbers
Test #3:
score: -100
Time Limit Exceeded
input:
50000 10 3 1 2 10 6 8 5 4 7 9 10 8 3 9 2 10 4 5 1 7 6 9 6 8 4 9 5 7 1 3 2 9 6 7 9 3 8 5 2 1 4 10 7 10 1 2 6 5 3 9 4 8 10 1 10 4 3 2 9 7 8 5 6 9 1 5 3 4 9 6 7 2 8 10 4 7 2 8 3 6 9 5 10 1 9 6 4 9 1 8 5 2 3 7 10 5 1 7 8 10 3 9 6 2 4 9 4 8 6 3 9 7 5 2 1 9 9 1 7 6 2 3 8 5 4 10 5 7 2 1 4 3 6 8 9 10 10 9 7...