QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#617888 | #1961. Postman | Cheek_support# | WA | 2ms | 8276kb | C++20 | 4.0kb | 2024-10-06 17:32:23 | 2024-10-06 17:32:24 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define mk(x,y) make_pair(x,y)
using namespace std;
int read(){
char ch=getchar();int x=0,f=1;
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return f*x;
}
int n,m;
const int N=300100;
ll a[N],b[N];
pair<ll ,int> c[N];
int x,y;
bool bo[N];
void work1(){
int L=m,R=n-m;
if(L==0){
if(x==1)printf("%lld\n",a[n]-a[1]);
else puts("-1");
return;
}
if(R==0){
if(x==n)printf("%lld\n",a[n]-a[1]);
else puts("-1");
return;
}
ll ans=(1ll<<60);
{
ll res=0;
int t=n-x-R;
if(t>0){
for(int i=1;i<=n-x-2;i++)
c[i]=mk(b[i+x],i+x);
sort(c+1,c+n-x-1);
ll num=0;res=(1ll<<60);
for(int i=1;i<=t;i++)
num+=c[i].first*2,bo[c[i].second]=1;
int i=t;
for(int j=n-1;j>=n-t;j--){
if(bo[j-1]){
bo[j-1]=0;
num+=b[j]-2*b[j-1];
}else{
for(;!bo[c[i].second];)i++;
bo[c[i].second]=1;
num+=b[j]-2*c[i].first;
}
res=min(res,num);
}
if(t==n-x-1)res=a[n]-a[x+1];
}
ans=min(ans,res+a[x]+a[n]-2*a[1]);
}
for(int i=1;i<=n;i++)a[i]=-a[i];
sort(a+1,a+n+1);
for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
x=n-x+1;y=n-y+1;
swap(L,R);
memset(bo,0,sizeof(bo));
{
ll res=0;
int t=n-x-R;
if(t>0){
for(int i=1;i<=n-x-2;i++)
c[i]=mk(b[i+x],i+x);
sort(c+1,c+n-x-1);
ll num=0;res=(1ll<<60);
for(int i=1;i<=t;i++)
num+=c[i].first*2,bo[c[i].second]=1;
int i=t;
for(int j=n-1;j>=n-t;j--){
if(bo[j-1]){
bo[j-1]=0;
num+=b[j]-2*b[j-1];
}else{
for(;!bo[c[i].second];)i++;
bo[c[i].second]=1;
num+=b[j]-2*c[i].first;
}
res=min(res,num);
}
if(t==n-x-1)res=a[n]-a[x+1];
}
ans=min(ans,res+a[x]+a[n]-2*a[1]);
}
printf("%lld\n",ans);
}
void work2(){
int L=m,R=n-m;
if(y<x){
for(int i=1;i<=n;i++)a[i]=-a[i];
sort(a+1,a+n+1);
for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
x=n-x+1;y=n-y+1;
swap(L,R);
}
if(R==0){puts("-1");return;}
if(L==1){
if(x==1&&y==n){
ll minn=(1ll<<60);
for(int i=2;i<n-1;i++)
minn=min(minn,b[i]);
if(minn==(1ll<<60))puts("-1");
else printf("%lld\n",a[n]-a[1]+2*minn);
return;
}
if(x==1||y==n)printf("%lld\n",2*(a[n]-a[1])-(a[y]-a[x]));
else puts("-1");
return;
}
if(L==0){
if(x==1&&y==n)printf("%lld\n",a[n]-a[1]);
else puts("-1");
return;
}
if(R==1){
if(y-x==1)printf("%lld\n",2*(a[n]-a[1])-(a[y]-a[x]));
else puts("-1");
return;
}
int t=y-x-R;
ll res=0;
if(t>0){
ll num=0;
for(int i=1;i<y-x-1;i++)
c[i]=mk(b[x+i],x+i);
sort(c+1,c+y-x-1);
for(int i=1;i<=t;i++)
num+=2*c[i].first;
res=num;
}
printf("%lld\n",res+2*(a[n]-a[1])-(a[y]-a[x]));
}
int main(){
n=read();m=read();
int k=read();
for(int i=1;i<=n;i++)a[i]=read();
x=0,y=a[n];
a[n+1]=0;n++;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
if(a[i]==x){x=i;break;}
for(int i=1;i<=n;i++)
if(a[i]==y){y=i;break;}
for(int i=1;i<n;i++)b[i]=a[i+1]-a[i];
if(k==1)work1();
else work2();
return 0;
}
/*
5 4 2
-20 -15 20 30 10
5 4 1
-20 -15 20 30 10
7 1 2
10 13 -30 24 50 -5 -21
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7904kb
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: -100
Wrong Answer
time: 2ms
memory: 8276kb
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:
1351
result:
wrong answer 1st lines differ - expected: '1397', found: '1351'