QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#730555 | #5143. Quick Sort | lllei# | WA | 36ms | 5712kb | C++14 | 2.1kb | 2024-11-09 20:40:08 | 2024-11-09 20:40:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n;
int a[N];
struct A {
int mx, mn;
} tree[N * 8];
void updata(int now) {
tree[now].mx = max(tree[now * 2].mx, tree[now * 2 + 1].mx);
tree[now].mn = min(tree[now * 2].mn, tree[now * 2 + 1].mn);
}
void bd(int now,int ll,int rr)
{
if(ll==rr)
{
tree[now].mx=tree[now].mn=a[ll];
return;
}
int mid=(ll+rr)/2;
bd(now*2,ll,mid);
bd(now*2+1,mid+1,rr);
updata(now);
}
void change(int x, int num, int now, int ll, int rr) {
if (x < ll || rr < x)return;
if (ll == rr && ll == x) {
tree[now].mx = tree[now].mn = num;
return;
}
int mid = (ll + rr) / 2;
change(x, num, now * 2, ll, mid);
change(x, num, now * 2 + 1, mid + 1, rr);
updata(now);
return;
}
int findl(int num, int now, int ll, int rr, int l, int r) {
if (ll > r || l > rr)return 0;
if (tree[now].mx < num) return 0;
if (ll == rr) {
return ll;
}
int mid = (ll + rr) / 2;
int tmp = findl(num, now * 2, ll, mid, l, r);
if (tmp) return tmp;
return findl(num, now * 2 + 1, mid + 1, rr, l, r);
}
int findr(int num, int now, int ll, int rr, int l, int r) {
if (ll > r || l > rr)return 0;
if (tree[now].mn > num) return 0;
if (ll == rr) {
return ll;
}
int mid = (ll + rr) / 2;
int tmp = findr(num, now * 2 + 1, mid + 1, rr, l, r);
if (tmp) return tmp;
return findr(num, now * 2, ll, mid, l, r);
}
long long st(int l,int r)
{
//cout<<l<<' '<<r<<endl;
if(l>=r)return 0;
long long tot=0;
int mid=(l+r)/2;
int ll=l,rr=r,pivot=a[mid];
int sp=0;
while(1)
{
int tol=findl(pivot,1,1,n,ll,n);
int tor=findr(pivot,1,1,n,1,rr);
if(tol>=tor)
{
sp=tor;
break;
}
change(tol,a[tor],1,1,n);
change(tor,a[tol],1,1,n);
swap(a[tol],a[tor]);
tot++;
ll=tol+1,rr=tor-1;
if(ll>=rr)
{
sp=rr;
break;
}
}
tot+=st(l,sp);
tot+=st(sp+1,r);
return tot;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
bd(1,1,n);
long long ans=st(1,n);
cout<<ans<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5628kb
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: -100
Wrong Answer
time: 36ms
memory: 5712kb
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 2 3 2 1 1 1 0 2 2 2 3 2 3 2 3 2 1 3 2 4 3 2 3 2 0 3 2 4 1 3 2 3 2 2 1 3 1 1 2 3 2 2 3 1 1 1 3 1 3 4 4 2 3 3 2 3 1 3 3 1 1 2 3 1 2 1 1 2 4 2 2 4 1 1 3 2 2 2 2 1 2 1 4 3 1 2 2 1 3 2 3 2 3 3 2 1 4 1 3 4 1 2 2 3 2 2 3 1 2 2 2 4 1 3 2 2 2 2 1 2 2 3 3 3 2 3 4 2 4 1 3 2 3 2 3 2 4 1 1 2 3 2 4 2 3 3 1 2 2 ...
result:
wrong answer 2nd numbers differ - expected: '4', found: '2'