![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
//
date : 5.4
2 // problem : dp
3 #include < iostream >
4 #include < cstdio >
5 #include < algorithm >
6 using namespace std;
7 struct node
8 {
9 int cost,w,nk;
10 }d[ 405 ];
11 int f[ 40005 ];
12 bool cmp(node a,node b)
13 {
14 return a.w <= b.w;
15 }
16 /// 模板
17 void one( int cost, int w, int m) // 完全背包
18 {
19 int i;
20 for (i = cost;i <= m;i ++ )
21 {
22 f[i] = max(f[i],f[i - cost] + w);
23 }
24 }
25
26 void one_zero( int cost, int w, int m) // 0/1背包
27 {
28 int i;
29 for (i = m;i >= cost;i -- )
30 {
31 f[i] = max(f[i],f[i - cost] + w);
32 }
33 }
34 void solve( int cost, int w, int nk)
35 {
36 if (cost * nk >= w) one(cost,cost,w); // 完全背包..
37 else // 不大于总重量的情况,就是 0/1 背包
38 {
39 int k = 1 ;
40 while (k < nk) // 把每件物品的个数转化成二进制
41 {
42 one_zero(k * cost,k * cost,w);
43 nk -= k;
44 k *= 2 ;
45 }
46 one_zero(nk * cost,nk * cost,w);
47 }
48 return ;
49 }
50 int main()
51 {
52 int n;
53 cin >> n;
54 int maxx =- 1 ;
55 for ( int i = 0 ;i < n;i ++ )
56 {
57 cin >> d[i].cost >> d[i].w >> d[i].nk;
58 maxx = max(maxx,d[i].w);
59 }
60 sort(d,d + n,cmp); // weight
61 for ( int i = 0 ;i < n;i ++ )
62 {
63 // int cost,w,nk;
64 // scanf("%d %d %d",&cost,&w,&nk); // 价值.重量.个数.
65 solve(d[i].cost,d[i].w,d[i].nk);
66 }
67 int maxxx = f[ 0 ];
68 for ( int i = 1 ;i <= maxx;i ++ )
69 {
70 if (f[i] > maxxx)
71 maxxx = f[i];
72 }
73 cout << maxxx << endl;
74 return 0 ;
75 }
2 // problem : dp
3 #include < iostream >
4 #include < cstdio >
5 #include < algorithm >
6 using namespace std;
7 struct node
8 {
9 int cost,w,nk;
10 }d[ 405 ];
11 int f[ 40005 ];
12 bool cmp(node a,node b)
13 {
14 return a.w <= b.w;
15 }
16 /// 模板
17 void one( int cost, int w, int m) // 完全背包
18 {
19 int i;
20 for (i = cost;i <= m;i ++ )
21 {
22 f[i] = max(f[i],f[i - cost] + w);
23 }
24 }
25
26 void one_zero( int cost, int w, int m) // 0/1背包
27 {
28 int i;
29 for (i = m;i >= cost;i -- )
30 {
31 f[i] = max(f[i],f[i - cost] + w);
32 }
33 }
34 void solve( int cost, int w, int nk)
35 {
36 if (cost * nk >= w) one(cost,cost,w); // 完全背包..
37 else // 不大于总重量的情况,就是 0/1 背包
38 {
39 int k = 1 ;
40 while (k < nk) // 把每件物品的个数转化成二进制
41 {
42 one_zero(k * cost,k * cost,w);
43 nk -= k;
44 k *= 2 ;
45 }
46 one_zero(nk * cost,nk * cost,w);
47 }
48 return ;
49 }
50 int main()
51 {
52 int n;
53 cin >> n;
54 int maxx =- 1 ;
55 for ( int i = 0 ;i < n;i ++ )
56 {
57 cin >> d[i].cost >> d[i].w >> d[i].nk;
58 maxx = max(maxx,d[i].w);
59 }
60 sort(d,d + n,cmp); // weight
61 for ( int i = 0 ;i < n;i ++ )
62 {
63 // int cost,w,nk;
64 // scanf("%d %d %d",&cost,&w,&nk); // 价值.重量.个数.
65 solve(d[i].cost,d[i].w,d[i].nk);
66 }
67 int maxxx = f[ 0 ];
68 for ( int i = 1 ;i <= maxx;i ++ )
69 {
70 if (f[i] > maxxx)
71 maxxx = f[i];
72 }
73 cout << maxxx << endl;
74 return 0 ;
75 }