QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379463 | #8572. Passing Game | ucup-team052# | WA | 510ms | 19000kb | C++23 | 2.0kb | 2024-04-06 17:35:01 | 2024-04-06 17:35:02 |
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==1||(r<n&&s[l]<=s[r])){
up(r,l);
if(r<n){
up(r,r+1);
++r;
}
}else{
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]);
}
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: 2ms
memory: 10016kb
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: -100
Wrong Answer
time: 510ms
memory: 19000kb
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:
106334086554860106
result:
wrong answer 1st numbers differ - expected: '31313390701066', found: '106334086554860106'