QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#768933 | #4802. Ternary Search | ship2077 | WA | 2ms | 7948kb | C++23 | 1.3kb | 2024-11-21 15:17:28 | 2024-11-21 15:17:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int M=3e5+5;
long long ans,pre[M],suf[M];
int n,m,a[M],tr[M],lsh[M];
int read(){
int x=0;char ch=getchar();
while (!isdigit(ch)) ch=getchar();
while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
return x;
}
void update(int x){while (x<=m) tr[x]++,x+=x&-x;}
int query(int x){int ans=0;while (x) ans+=tr[x],x-=x&-x;return ans;}
long long getans(int n){ long long ans=1ll<<60;
for (int i=1;i<=m;i++) tr[i]=0;
for (int i=1;i<=n;i++) update(a[i]),pre[i]=pre[i-1]+i-query(a[i]);
for (int i=1;i<=m;i++) tr[i]=0;
for (int i=n;i;i--) suf[i]=suf[i+1]+n-i-query(a[i]),update(a[i]);
for (int i=1;i<=n;i++) ans=min(ans,pre[i]+suf[i+1]);
for (int i=1;i<=n;i++) a[i]=m+1-a[i];
for (int i=1;i<=m;i++) tr[i]=0;
for (int i=1;i<=n;i++) update(a[i]),pre[i]=pre[i-1]+i-query(a[i]);
for (int i=1;i<=m;i++) tr[i]=0;
for (int i=n;i;i--) suf[i]=suf[i+1]+n-i-query(a[i]),update(a[i]);
for (int i=1;i<=n;i++) ans=min(ans,pre[i]+suf[i+1]);
for (int i=1;i<=n;i++) a[i]=m+1-a[i];
return ans;
}
int main(){ n=read();
for (int i=1;i<=n;i++) a[i]=lsh[i]=read();
sort(lsh+1,lsh+n+1);m=unique(lsh+1,lsh+n+1)-lsh-1;
for (int i=1;i<=n;i++) a[i]=lower_bound(lsh+1,lsh+m+1,a[i])-lsh;
for (int i=1;i<=n;i++) printf("%lld\n",getans(i));
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7876kb
input:
9 11 4 5 14 1 9 19 8 10
output:
0 0 0 0 2 3 3 6 7
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 7944kb
input:
1 167959139
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5908kb
input:
10 802030518 983359971 96204862 640274071 796619138 71550121 799843967 598196518 402690754 446173607
output:
0 0 0 1 1 3 3 5 7 9
result:
ok 10 numbers
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 7948kb
input:
200 280383943 994080326 443906342 107926501 414713819 45732490 370693588 928126378 581740643 189811400 142817640 218386999 447997927 640003704 637428425 151698689 209462128 155599305 390080477 958251194 515653943 751042490 1186761 443171900 968975479 829084542 462837689 157692260 607049612 606116398...
output:
0 0 0 0 1 1 3 4 5 8 12 15 17 18 19 25 30 35 38 38 41 42 54 60 60 62 69 82 88 95 110 113 124 126 139 148 152 164 169 180 187 193 212 222 226 244 244 252 265 274 287 308 317 329 340 359 380 394 410 418 440 446 448 464 491 504 522 537 548 569 597 622 640 640 661 670 704 726 731 760 774 779 818 839 849 ...
result:
wrong answer 28th numbers differ - expected: '81', found: '82'