标签:for i+1 clu ret c++ const code 背包 ons
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1005;
const int M=10005;
int n,m,a[N],b[N],c[N];
int dp[N][M];//dp[i,j]从前i个中拿,体积为j的最大价值
int dp1[N][M];//dp1[i,j]从后i个中拿,体积为j的最大价值
int q[M],hh,tt;
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i]>>b[i]>>c[i];
for(int i=1; i<=n; i++)
for(int j=0; j<=a[i]-1; j++)
{
hh=0,tt=-1;
for(int k=j; k<=M; k+=a[i])
{
while(hh<=tt && q[hh]<k-c[i]*a[i])
hh++;
while(hh<=tt && dp[i-1][q[tt]]-(q[tt])/a[i]*b[i]<dp[i-1][k]-(k)/a[i]*b[i])
tt--;
q[++tt]=k;
if(hh<=tt)
dp[i][k]=dp[i-1][q[hh]]+(k-q[hh])/a[i]*b[i];
}
}
for(int i=n; i; --i)
for(int j=a[i]-1; j>=0; j--)
{
hh=0,tt=-1;
for (int k=j; k<M; k+=a[i])
{
while(hh<=tt && q[hh]<k-c[i]*a[i])
hh++;
while(hh<=tt&& dp1[i+1][q[tt]]-(q[tt]-j)/a[i]*b[i]<dp1[i+1][k]-(k-j)/a[i]*b[i])
tt--;
q[++tt]=k;
dp1[i][k]=dp1[i+1][q[hh]]+(k-q[hh])/a[i]*b[i];
}
}
cin>>m;
while(m--)
{
int x,y,ans=0;
cin>>x>>y;
x+=1;
for(int i=0; i<=y; i++)
ans=max(ans,dp[x-1][i]+dp1[x+1][y-i]);
printf("%d\n",ans);
}
return 0;
}
luogu P4095 [HEOI2013]Eden 的新背包问题 分治+背包dp
标签:for i+1 clu ret c++ const code 背包 ons
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12828058.html