QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379560 | #8572. Passing Game | ucup-team052# | WA | 492ms | 19124kb | C++23 | 2.0kb | 2024-04-06 17:51:38 | 2024-04-06 17:51:39 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
using LL=long long;
const int N=300005;
int T,n,K,x[N],s[N],id[N],id_[N];
struct node{
LL x;
int k;
node operator+(const node&rhs)const{return (node){x+rhs.x,k+rhs.k};}
bool operator<(const node&rhs)const{
return tie(x,k)<tie(rhs.x,rhs.k);
}
};
node dp[N][2];
node DP(LL mid,int op){
rep(i,1,n){
dp[i][0]=dp[i][1]=(node){LL(1e18),0};
}
int p1=id_[1];
dp[p1][0]=(node){0,0};
dp[p1][1]=(node){0,0};
int l=p1-1,r=p1+1;
auto up=[&](int u,int v){
int o1=u<v?1:0;
rep(o2,0,1){
dp[v][o1]=min(dp[v][o1],(node){dp[u][o2].x+1LL*abs(x[u]-x[v])*s[u]+(o1!=o2?mid:0),dp[u][o2].k+(o1!=o2?1:0)});
}
};
if(l>=1)up(p1,l);
if(r<=n)up(p1,r);
while(l>=1||r<=n){
if(l==0||(r<=n&&s[l]<=s[r])){
if(l)up(r,l);
if(r<n){
up(r,r+1);
}
++r;
}else{
if(r<=n)up(l,r);
if(l>1){
up(l,l-1);
}
--l;
}
}
up(1,n),up(n,1);
rep(i,1,n)up(i,id_[n]);
return dp[id_[n]][op];
}
signed main(){
#ifdef xay5421
freopen("a.in","r",stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin>>T;
while(T--){
cin>>n>>K;
rep(i,1,n){
cin>>x[i];
}
rep(i,1,n){
cin>>s[i];
}
rep(i,1,n)id[i]=i;
sort(id+1,id+n+1,[&](int lhs,int rhs){return x[lhs]<x[rhs];});
rep(i,1,n)id_[id[i]]=i;
{
static int tmp[N];
rep(i,1,n)tmp[i]=x[i];
rep(i,1,n)x[id_[i]]=tmp[i];
rep(i,1,n)tmp[i]=s[i];
rep(i,1,n)s[id_[i]]=tmp[i];
}
per(i,id_[1]-1,1){
s[i]=min(s[i],s[i+1]);
}
rep(i,id_[1]+1,n){
s[i]=min(s[i],s[i-1]);
}
// auto ret=DP(0,0);
// cerr<<ret.x<<' '<<ret.k<<'\n';
LL ans=9e18;
rep(op,0,1){
LL l=0,r=LL(1e18)+5,res=0;
while(l<=r){
// cerr<<l<<' '<<r<<'\n';
LL mid=(l+r)>>1;
auto ret=DP(mid,op);
if(ret.k<=K)r=mid-1,res=mid;
else l=mid+1;
}
auto ret=DP(res,op);
ans=min(ans,ret.x-1LL*K*res);
}
printf("%lld\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10028kb
input:
2 4 2 3 2 1 6 3 1 1 3 2 0 1 2 1 2
output:
7 1
result:
ok 2 number(s): "7 1"
Test #2:
score: 0
Accepted
time: 459ms
memory: 19048kb
input:
1 300000 204334 809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...
output:
31313390701066
result:
ok 1 number(s): "31313390701066"
Test #3:
score: 0
Accepted
time: 492ms
memory: 14288kb
input:
3 100000 65460 217141764 710454586 789075415 24849107 685675008 839804815 638763480 327755609 43827967 390187172 301370841 622696676 598237196 232099091 211987715 416876077 572665966 73382836 520033984 808399404 752832432 341795744 434460344 535426588 136624537 997406768 297342165 558882675 26863877...
output:
70635841128944 47230361360721 59110547802683
result:
ok 3 number(s): "70635841128944 47230361360721 59110547802683"
Test #4:
score: 0
Accepted
time: 453ms
memory: 19124kb
input:
1 300000 101975 207258305 525434317 528778163 645316642 562113679 143398489 9114413 669854123 106324041 841914487 21419012 308025536 689200225 263298218 39377353 860366080 24610184 43404209 529054797 902238799 422737070 484129934 967667618 953541323 338625285 115085955 363490839 998893783 877857789 ...
output:
40311829457542
result:
ok 1 number(s): "40311829457542"
Test #5:
score: -100
Wrong Answer
time: 2ms
memory: 9992kb
input:
18 17 0 500000000 499999997 500000010 499999965 500000118 499999609 500001291 499995739 500014064 499953589 500153157 499494579 501667889 494495965 518163316 440061055 697798520 197798520 59938945 18163316 5504035 1667889 505421 153157 46411 14064 4261 1291 391 118 35 10 3 1 17 1 500000000 499999997...
output:
15506866876 14901483521 14749041487 14596599454 14462047676 14327495899 14198662458 14069829018 13942062276 13814295534 13686666452 13559037370 13431426532 13303815695 13182757177 13061698660 13061698660 13061698660
result:
wrong answer 3rd numbers differ - expected: '14901483521', found: '14749041487'