bzoj3163

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<string>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<iomanip>
 8 using namespace std;
 9 //f[i][j]:前i个物品占j空间的收益 
10 //g[i][j]:后i个物品占j空间的收益 
11 namespace Moxing{
12     const int N=1005;
13     int n,q;
14     struct node{
15         int a,b,c;
16     }toy[N];
17     long long f[N][N],g[N][N];
18     void work(){
19         for(int i=1,tmp;i<=n;i++){
20             tmp=toy[i].c;
21             for(int j=1;j<N;j++){
22                 f[i][j]=f[i-1][j];
23             }
24             for(int j=1;tmp!=0;j<<=1){
25                 j=min(j,tmp);
26                 tmp-=j;
27                 for(int k=N-1;k>=j*toy[i].a;k--){
28                     f[i][k]=max(f[i][k],f[i][k-j*toy[i].a]+j*toy[i].b);
29                 }
30             }
31         }
32         for(int i=n,tmp;i>=1;i--){
33             tmp=toy[i].c;
34             for(int j=1;j<N;j++){
35                 g[i][j]=g[i+1][j];
36             }
37             for(int j=1;tmp!=0;j<<=1){
38                 j=min(j,tmp);
39                 tmp-=j;
40                 for(int k=N-1;k>=j*toy[i].a;k--){
41                     g[i][k]=max(g[i][k],g[i][k-j*toy[i].a]+j*toy[i].b);
42                 }
43             }
44         }
45     }
46     struct main{
47         main(){
48             scanf("%d",&n);
49             for(int i=1;i<=n;i++){
50                 scanf("%d%d%d",&toy[i].a,&toy[i].b,&toy[i].c);//, bi, c i,分别表示买一个第i个玩偶需要的价钱,获得的价值以及第i个玩偶的限购次数。 
51             }
52             scanf("%d",&q);
53             work();
54             for(int i=1;i<=q;i++){
55                 int d,e;//从0开始编号 
56                 scanf("%d%d",&d,&e);d++;
57                 long long ans=0;
58                 for(int j=0;j<=e;j++){
59                     ans=max(ans,f[d-1][j]+g[d+1][e-j]);
60                 }
61                 printf("%lld\n",ans);
62             }
63             
64             exit(0);
65         }
66     }UniversalLove;
67 } 
68 int main(){
69     Moxing::main(); 
70 }
View Code

猜你喜欢

转载自www.cnblogs.com/Moxingtianxia/p/11366664.html