题解:P11924 [PA 2025] 贪婪大盗 / Piracka Chciwość
考虑从后往前 dp,dp 到
注意到这个题给了 6s,考虑优化掉一只
时间复杂度
#include<bits/stdc++.h>
using namespace std;
const int M=50005;
int rd(){
char c=getchar();
int res=0;
while(c>'9'||c<'0')c=getchar();
while(c<'9'&&c>='0')res=((res<<1)+(res<<3)+c-'0'),c=getchar();
return res;
}
void wt(int x){
if(x>9)wt(x/10);
putchar(x%10+'0');
}
int n,m,a[M],f[M],s[5000505];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
f[n]=m+a[n];
if(f[n]<=64)s[f[n]]++;
for(int i=n-1;i>=1;--i){
int nd=(n-i)/2,nw=0,maxn=0,sum=0;
for(int p=0;p<=64;++p){
if(nw<nd){
if(nw+s[p]<=nd)sum+=p*s[p];
else sum+=(nd-nw)*p;
nw+=s[p],maxn=p;
if(nw<nd)nw-=s[p],nd-=s[p];
}
}
if(sum>m){
f[i]=0;
s[0]++;
}else{
for(int j=0;j<=64;++j)s[j]=0;
for(int j=n;j>i;--j){
if(f[j]<maxn){
f[j]+=a[j];
}else if(f[j]==maxn&&nd){
nd--;f[j]+=a[j];
}else f[j]=a[j];
s[f[j]]++;
}
f[i]=m-sum+a[i];
s[f[i]]++;
}
}
for(int i=1;i<=n;++i){
if(f[i]-a[i]<0){
putchar('-');
putchar('1');
}else wt(f[i]-a[i]);
putchar(' ');
}
return 0;
}