QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#859228 | #9679. 盒子 | JohnAlfnov | 0 | 3ms | 29700kb | C++17 | 3.0kb | 2025-01-17 16:39:32 | 2025-01-17 16:39:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int a[500005];
long long S[500005];
long long f[500005];
int R[500005];
int sm[15000005],ls[15000005],rs[15000005],tot=0;
void fuzhi(int l,int r,int o,int ll,int rr,int z){
if(l>=ll&&r<=rr){
sm[o]=min(sm[o],z);
return;
}
int mid=(l+r)>>1;
if(mid>=ll){
if(!ls[o])ls[o]=++tot,sm[tot]=1e9;
fuzhi(l,mid,ls[o],ll,rr,z);
}
if(mid<rr){
if(!rs[o])rs[o]=++tot,sm[tot]=1e9;
fuzhi(mid+1,r,rs[o],ll,rr,z);
}
}
int qiu(int l,int r,int o,int x){
if(!o)return 1e9;
if(l==r)return sm[o];
int mid=(l+r)>>1,ans=sm[o];
if(x<=mid)ans=min(ans,qiu(l,mid,ls[o],x));
else ans=min(ans,qiu(mid+1,r,rs[o],x));
return ans;
}
int n,m,k,c;
long long s1[15000005],s2[15000005];
vector<int>g[500005];
void add(int l,int r,int o,int x,long long t1,long long t2){
if(l==r){
s1[o]=min(s1[o],t1);s2[o]=min(s2[o],t2);
return;
}
int mid=(l+r)>>1;
if(x<=mid){
if(!ls[o])ls[o]=++tot,s1[tot]=s2[tot]=1e18;
add(l,mid,ls[o],x,t1,t2);
s1[o]=min(s1[o],s1[ls[o]]);
s2[o]=min(s2[o],s2[ls[o]]);
}else{
if(!rs[o])rs[o]=++tot,s1[tot]=s2[tot]=1e18;
add(mid+1,r,rs[o],x,t1,t2);
s1[o]=min(s1[o],s1[rs[o]]);
s2[o]=min(s2[o],s2[rs[o]]);
}
}
long long m1,m2;
void query(int l,int r,int o,int ll,int rr){
if(!o)return;
if(l>=ll&&r<=rr){
m1=min(m1,s1[o]);m2=min(m2,s2[o]);
return;
}
int mid=(l+r)>>1;
if(mid>=ll)query(l,mid,o<<1,ll,rr);
if(mid<rr)query(mid+1,r,o<<1|1,ll,rr);
}
void solve(int l,int r){
if(l==r)return;
int mid=(l+r)>>1;
solve(mid+1,r);
for(int i=1;i<=tot;++i)ls[i]=rs[i]=0;
tot=1;s1[tot]=s2[tot]=1e18;
for(int i=mid+1;i<=r;++i)g[i].clear();
for(int i=l;i<=mid;++i)if(R[i]>=mid+1){
g[min(r,R[i])].emplace_back(i);
}
for(int j=mid+1;j<=r;++j){
add(0,k-1,1,S[j]%k,f[j]+S[j]/k*c,f[j]+S[j]/k*c+S[j]%k);
for(auto i:g[j]){
if(S[i]%k>0){
m1=m2=1e18;
query(0,k-1,1,0,S[i]%k-1);
f[i]=min(f[i],m1-S[i]/k*c);
f[i]=min(f[i],m2-S[i]/k*c-c+k-S[i]%k);
}
if(S[i]%k<k){
m1=m2=1e18;
query(0,k-1,1,S[i]%k,k-1);
f[i]=min(f[i],m1-S[i]/k*c+c);
f[i]=min(f[i],m2-S[i]/k*c-S[i]%k);
}
}
}
solve(l,mid);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&m,&k,&c);
for(int i=1;i<=n;++i)scanf("%d",&a[i]),S[i]=S[i-1]+a[i];
if(m==1){
long long ans=0;
for(int i=1;i<=n;++i){
ans+=1ll*a[i]/k*c;
ans+=min(c,a[i]%k);
}
printf("%lld\n",ans);
continue;
}
for(int i=0;i<=n;++i)f[i]=1e18;
f[n]=0;
for(int i=1;i<=tot;++i)sm[i]=ls[i]=rs[i]=0;
tot=1;sm[1]=n;
for(int i=n;i>=1;--i){
if(i+m<=n){
int j=i+m;
long long z=k+S[j-m]-S[j-1]-1;
if(z>=0){
int L=(S[j-m]-z)%k,R=S[j-m]%k;
if(L<0)L+=k;
if(R<0)R+=k;
if(L<=R){
fuzhi(0,k-1,1,L,R,j-1);
}else{
fuzhi(0,k-1,1,L,k-1,j-1);
fuzhi(0,k-1,1,0,R,j-1);
}
}
}
R[i-1]=qiu(0,k-1,1,S[i-1]%k);
}
solve(0,n);
printf("%lld\n",f[0]);
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 29700kb
input:
3 5 2 4 3 2 2 1 2 2 4 2 4 3 2 4 1 1 10 3 5 1 2 2 2 2 1 1 1 10 2 2
output:
0 -1 -3
result:
wrong answer 1st numbers differ - expected: '7', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Runtime Error
Test #35:
score: 0
Runtime Error
input:
66664 7 2 82188055 1 35930054 4923258 36288509 46890418 53350617 49812938 68015568 10 2 460335201 1 305598063 240803174 36008172 416771728 391050572 270293987 333994588 436573185 216917970 103343453 9 3 119910901 1 35106715 29444257 72409421 49339248 23617992 3266647 38704192 75874356 72979434 10 1 ...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%