QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546958 | #1961. Postman | ucup-team052# | WA | 14ms | 5984kb | C++23 | 2.4kb | 2024-09-04 16:15:03 | 2024-09-04 16:15:03 |
Judging History
answer
#include<bits/stdc++.h>
#define D(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
using namespace std;
using LL=long long;
const int N=100005;
int n,w,t,id[N],_id[N];
LL x[N],ans[N];
struct DS{
priority_queue<LL>q1;
priority_queue<LL,vector<LL>,greater<LL> >q2;
LL s;
void clear(){
// D("clear\n");
s=0;
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop();
}
int size()const{return SZ(q1)+SZ(q2);}
void push(int x){
// D("push %d\n",x);
q2.push(x);
}
LL query(int k){
while(SZ(q1)<k){
s+=q2.top();
q1.push(q2.top());
q2.pop();
}
while(SZ(q1)>k){
s-=q1.top();
q2.push(q1.top());
q1.pop();
}
if(SZ(q1)&&SZ(q2)){
while(q1.top()>q2.top()){
LL x=q1.top();
LL y=q2.top();
s=s-x+y;
q1.pop();
q2.pop();
q1.push(y),q2.push(x);
}
}
assert(SZ(q1)==k);
// D("query %d = %lld\n",k,s);
return s;
}
}ds;
void solve(int w,int e){
ds.clear();
rep(i,0,n)id[i]=i;
sort(id,id+n+1,[&](int lhs,int rhs){return x[lhs]<x[rhs];});
rep(i,0,n)_id[id[i]]=i;
int bg=_id[0];
rep(ed,0,n)if(ed!=bg){ // id[ed] at x[id[ed]]
if(ed<bg){
int least_w=(bg!=0)+(ed!=n);
int least_e=1;
if(w>=least_w&&e>=least_e){
ans[id[ed]]=min(ans[id[ed]],x[id[bg]]-x[id[0]]+x[id[n]]-x[id[0]]+x[id[n]]-x[id[ed]]);
}
}else{
int least_w=(bg!=0)+(ed!=n);
int most_w=bg+n-ed;
if(w>=least_w&&w<=most_w){
ans[id[ed]]=min(ans[id[ed]],x[id[bg]]-x[id[0]]+x[id[n]]-x[id[0]]+x[id[n]]-x[id[ed]]);
}
if(w>most_w){
if(w<=most_w+ds.size()){
ans[id[ed]]=min(ans[id[ed]],x[id[bg]]-x[id[0]]+x[id[n]]-x[id[0]]+x[id[n]]-x[id[ed]]+ds.query(w-most_w)*2);
}
}
}
if(ed-1>bg){
ds.push(x[id[ed]]-x[id[ed-1]]);
}
}
}
int main(){
#ifdef xay5421
freopen("a.in","r",stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>w>>t;
x[0]=0;
rep(i,1,n)cin>>x[i];
memset(ans,63,sizeof(ans));
solve(w,n-w);
rep(i,0,n)x[i]*=-1;
solve(n-w,w);
LL res;
if(t==2){
res=ans[n];
}else{
res=*min_element(ans+1,ans+n+1);
}
if(res>=1e18){
puts("-1");
}else{
printf("%lld\n",res);
}
return 0;
}
/*
3 2 2
-1 -6 -8
clear
clear
push 5
query 1 = 5
push 2
13
ans=18
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5984kb
input:
100 0 1 751 558 131 292 317 886 785 847 224 668 649 358 725 250 953 65 176 733 461 84 114 977 628 673 798 863 373 827 51 630 518 347 527 876 366 241 452 670 813 314 846 342 357 460 985 581 841 843 282 907 327 656 411 944 421 99 441 388 568 763 167 351 793 916 517 109 999 390 272 639 513 534 304 253 ...
output:
999
result:
ok single line: '999'
Test #2:
score: 0
Accepted
time: 0ms
memory: 4716kb
input:
100 50 1 654 349 533 94 619 754 498 939 469 518 3 661 431 214 195 198 989 548 561 312 262 629 807 981 624 639 178 765 706 782 888 611 553 363 336 91 176 485 735 791 596 455 390 760 995 202 417 82 798 883 209 155 725 908 789 56 49 899 231 423 815 228 314 400 748 946 736 840 385 446 938 284 10 917 593...
output:
1397
result:
ok single line: '1397'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4668kb
input:
100 99 1 266 337 80 671 358 778 217 288 219 48 863 19 137 987 602 415 155 398 753 956 210 317 94 303 997 192 123 442 666 959 685 71 34 588 615 214 327 881 582 51 646 630 291 354 749 108 984 308 813 182 384 699 114 692 611 296 590 432 103 240 341 465 969 370 771 914 250 656 742 708 174 323 939 493 17...
output:
1986
result:
ok single line: '1986'
Test #4:
score: 0
Accepted
time: 1ms
memory: 4540kb
input:
100 100 1 713 172 634 763 181 502 78 995 598 751 792 961 585 335 952 343 375 627 651 515 856 121 743 931 18 492 726 517 136 143 314 993 80 29 283 427 248 274 114 200 535 94 174 782 818 90 806 88 104 808 910 892 791 462 304 676 67 973 901 891 705 567 475 444 949 846 299 735 233 914 803 27 810 210 396...
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 1ms
memory: 4372kb
input:
100 0 1 -102 -827 -638 -660 -604 -258 -692 -237 -927 -371 -129 -908 -830 -229 -585 -364 -103 -58 -975 -153 -563 -17 -201 -443 -56 -560 -606 -45 -891 -331 -349 -420 -266 -219 -482 -716 -51 -723 -876 -211 -121 -63 -814 -239 -937 -541 -74 -69 -334 -42 -227 -479 -571 -137 -913 -885 -525 -475 -930 -405 -...
output:
-1
result:
ok single line: '-1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4772kb
input:
100 1 1 -747 -27 -215 -283 -833 -584 -531 -295 -636 -725 -100 -506 -108 -426 -574 -305 -919 -207 -540 -770 -264 -67 -388 -444 -77 -778 -474 -957 -706 -467 -438 -245 -240 -228 -40 -523 -803 -669 -198 -11 -740 -911 -990 -604 -236 -628 -864 -897 -440 -789 -874 -941 -153 -1 -52 -345 -819 -480 -731 -612 ...
output:
1979
result:
ok single line: '1979'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4660kb
input:
100 50 1 -220 -474 -778 -792 -88 -62 -252 -742 -147 -2 -119 -575 -583 -434 -753 -425 -427 -266 -419 -344 -44 -470 -79 -28 -38 -621 -506 -821 -363 -423 -960 -272 -608 -728 -482 -392 -194 -958 -395 -712 -97 -867 -34 -499 -101 -777 -895 -200 -269 -528 -283 -972 -157 -378 -561 -90 -359 -439 -412 -72 -87...
output:
1433
result:
ok single line: '1433'
Test #8:
score: 0
Accepted
time: 1ms
memory: 4712kb
input:
100 100 1 -100 -438 -107 -887 -504 -162 -557 -696 -551 -955 -634 -230 -109 -3 -754 -669 -775 -685 -517 -137 -80 -419 -207 -131 -554 -26 -864 -981 -282 -925 -704 -457 -186 -975 -730 -174 -294 -176 -664 -385 -625 -894 -93 -156 -737 -541 -265 -906 -403 -445 -41 -260 -609 -828 -60 -800 -750 -6 -239 -846...
output:
981
result:
ok single line: '981'
Test #9:
score: -100
Wrong Answer
time: 14ms
memory: 5948kb
input:
300000 299900 1 975494913 919276412 971822415 306295652 981185393 751782065 954706942 908207836 981200361 32580760 933121007 333012429 165323270 996792016 391102892 389040132 322687143 812406210 317257318 897931477 984765477 903680189 380567163 385513860 359332788 768879270 959852716 843574823 40665...
output:
-1
result:
wrong answer 1st lines differ - expected: '2999995655', found: '-1'