Codeforces Round #105 (Div. 2) E. Porcelain —— DP(背包问题)

题目链接:http://codeforces.com/problemset/problem/148/E


 

E. Porcelain
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.

The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.

You are given the values of all items. Your task is to find the maximal damage the princess' tantrum of m shrieks can inflict on the collection of porcelain.

Input

The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.

Output

Output the maximal total value of a tantrum of m shrieks.

Examples
input
2 3
3 3 7 2
3 4 1 5
output
15
input
1 3
4 4 3 1 2
output
9
Note

In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.

In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.

 

 



题解:


 

方法一:

1.对于每个书架,求出每种长度下的首尾相连的最大连续和。

2.通过类似解决背包问题的方法,求得在这些最大连续和的组合下的最大值,即为答案。

 


方法二:

反向思维:求两边的和最大,即求中间的和最小。

1.对于每个书架,求出每种长度下的最小连续和。

2.通过类似解决背包问题的方法,求得在这些最小连续和的组合下的最小值,然后再用总和减去这个最小值,即为答案。



写法:

i为第i个书架, j为dp到当前待组合的个数, k则为对于每一个书架相应的连续个数。

写法一:从小往大推,此时dp数组需要开二维。

写法二:从大往小推,此时dp数组只需开一维。

注意:写法二必须保证这一阶段的数据只能从上一阶段的数据转移过来, 而不能从同一阶段转移过来,否则会出现重复。



 

方法一写法一:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e4+10;
 9 
10 int n, m;
11 int dp[105][maxn];
12 int a[105][105], sum[105][105], s[105][105];
13 
14 void init()
15 {
16     scanf("%d%d",&n, &m);
17     for(int i = 1; i<=n; i++)
18     {
19         scanf("%d",&a[i][0]);
20         for(int j = 1; j<=a[i][0]; j++)
21             scanf("%d",&a[i][j]);
22     }
23 
24     for(int i = 1; i<=n; i++)   //求前缀和
25     for(int j = 1; j<=a[i][0]; j++)
26         sum[i][j] = sum[i][j-1] + a[i][j];
27 
28     for(int i = 1; i<=n; i++)   //求每个书架每种长度下,首尾相连序列的最大连续和
29     for(int l = 0; l<=a[i][0]; l++)
30     for(int r = 0; l+r<=a[i][0]; r++)
31         s[i][l+r] = max(s[i][l+r], sum[i][l] + sum[i][a[i][0]] - sum[i][a[i][0]-r]);
32 }
33 
34 void solve()
35 {
36     for(int i = 1; i<=n; i++)  //01背包的拓展,每个状态都从O(n)转移过来, 而普通背包则为O(1)
37     {
38         for(int j = 1; j<=m; j++)   //别忘了上一阶段的j状态也参与当前阶段j状态的转移
39             dp[i][j] = dp[i-1][j];
40 
41         for(int j = 1; j<=m; j++)
42         for(int k = 1; k<=min(j,a[i][0]); k++)
43             dp[i][j] = max(dp[i][j], s[i][k]+dp[i-1][j-k]);
44     }
45     cout<<dp[n][m]<<endl;
46 }
47 
48 int main()
49 {
50     init();
51     solve();
52 }
View Code

 

方法一写法二:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e4+10;
 9 
10 int n, m;
11 int dp[maxn];
12 int a[105][105], sum[105][105], s[105][105];
13 
14 void init()
15 {
16     scanf("%d%d",&n, &m);
17     for(int i = 1; i<=n; i++)
18     {
19         scanf("%d",&a[i][0]);
20         for(int j = 1; j<=a[i][0]; j++)
21             scanf("%d",&a[i][j]);
22     }
23 
24     for(int i = 1; i<=n; i++)
25     for(int j = 1; j<=a[i][0]; j++)
26         sum[i][j] = sum[i][j-1] + a[i][j];
27 
28     for(int i = 1; i<=n; i++)
29     for(int l = 0; l<=a[i][0]; l++)
30     for(int r = 0; l+r<=a[i][0]; r++)
31         s[i][l+r] = max(s[i][l+r], sum[i][l] + sum[i][a[i][0]] - sum[i][a[i][0]-r]);
32 }
33 
34 void solve()
35 {
36     for(int i = 1; i<=n; i++)
37     for(int j = m; j>=1; j--)
38     for(int k = 1; k<=min(j,a[i][0]); k++)
39         dp[j] = max(dp[j], s[i][k]+dp[j-k]);
40 
41     cout<<dp[m]<<endl;
42 }
43 
44 int main()
45 {
46     init();
47     solve();
48 }
View Code

 

方法二写法一:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e4+10;
 9 
10 int n, m, all;
11 int dp[105][maxn];
12 int a[105][105], sum[105][105], s[105][105];
13 
14 void init()
15 {
16     scanf("%d%d",&n, &m);
17     m = -m;
18     for(int i = 1; i<=n; i++)
19     {
20         scanf("%d",&a[i][0]);
21         for(int j = 1; j<=a[i][0]; j++)
22             scanf("%d",&a[i][j]), all += a[i][j];
23         m += a[i][0];
24     }
25 
26     for(int i = 1; i<=n; i++)   //前缀和
27     for(int j = 1; j<=a[i][0]; j++)
28         sum[i][j] = sum[i][j-1] + a[i][j];
29 
30     for(int i = 1; i<=n; i++)   //最小连续和,因为“最小”,所以需要初始化为最大
31     for(int j = 1; j<=a[i][0]; j++)
32         s[i][j] = INF;
33 
34     for(int i = 1; i<=n; i++)   //求最小连续和
35     for(int j = 1; j<=a[i][0]; j++)
36     for(int k = 1; k<=j; k++)
37         s[i][j-k+1] = min(s[i][j-k+1], sum[i][j]-sum[i][k-1]);
38 }
39 
40 void solve()
41 {
42     for(int i = 0; i<=n; i++)   //需要所有都初始化为最大
43     for(int j = 1; j<=m; j++)
44         dp[i][j] = INF;
45 
46     for(int i = 1; i<=n; i++)
47     {
48         for(int j = 1; j<=m; j++)
49             dp[i][j] = dp[i-1][j];
50 
51         for(int j = 1; j<=m; j++)
52         for(int k = 1; k<=min(j,a[i][0]); k++)
53             dp[i][j] = min(dp[i][j], s[i][k]+dp[i-1][j-k]);
54     }
55     cout<<all - dp[n][m]<<endl;
56 }
57 
58 int main()
59 {
60     init();
61     solve();
62 }
View Code

 

方法二写法二:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e4+10;
 9 
10 int n, m, all;
11 int dp[maxn];
12 int a[105][105], sum[105][105], s[105][105];
13 
14 void init()
15 {
16     scanf("%d%d",&n, &m);
17     m = -m;
18     for(int i = 1; i<=n; i++)
19     {
20         scanf("%d",&a[i][0]);
21         for(int j = 1; j<=a[i][0]; j++)
22             scanf("%d",&a[i][j]), all += a[i][j];
23         m += a[i][0];
24     }
25 
26     for(int i = 1; i<=n; i++)
27     for(int j = 1; j<=a[i][0]; j++)
28         sum[i][j] = sum[i][j-1] + a[i][j];
29 
30     for(int i = 1; i<=n; i++)
31     for(int j = 1; j<=a[i][0]; j++)
32         s[i][j] = INF;
33 
34     for(int i = 1; i<=n; i++)
35     for(int j = 1; j<=a[i][0]; j++)
36     for(int k = 1; k<=j; k++)
37         s[i][j-k+1] = min(s[i][j-k+1], sum[i][j]-sum[i][k-1]);
38 }
39 
40 void solve()
41 {
42     for(int j = 1; j<=m; j++)
43         dp[j] = INF;
44 
45     for(int i = 1; i<=n; i++)
46     for(int j = m; j>=1; j--)
47     for(int k = 1; k<=min(j,a[i][0]); k++)
48         dp[j] = min(dp[j], s[i][k]+dp[j-k]);
49 
50     cout<<all-dp[m]<<endl;
51 }
52 
53 int main()
54 {
55     init();
56     solve();
57 }
View Code

 

posted on 2017-06-18 20:55  h_z_cong  阅读(247)  评论(0编辑  收藏  举报

导航