题意:第一行给出两个数n,m,n表示下面有n行数据,每行数据可以从前或从后去若干个数,一共取m个数,求取出的n个数和最大值
链接:http://codeforces.com/problemset/problem/148/E
思路:预处理背包,将每行数据预处理,求出每行处理成max_n = ( left_sum_n, right_sum_n, left_right_sum_n ),后面按正常背包处理即可
注意点:无
以下为AC代码:
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged |
---|---|---|---|---|---|---|---|---|
9742020 | Practice: luminous11 | 148E - 12 | GNU C++11 | Accepted | 278 ms | 796 KB | 2015-02-07 07:59:22 | 2015-02-07 07:59:22 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
int tmp[100005] = { 0 };
int dp[100005] = { 0 };
int num[105] = { 0 };
int ls[105] = { 0 };
int rs[105] = { 0 };
int ss[105] = { 0 };
int main()
{
ios::sync_with_stdio( false );
int m, n;
while ( cin >> n >> m ){
int k;
clr ( dp, 0 );
clr ( num, 0 );
for ( int i = 0; i < n; i ++ ){
cin >> k;
for ( int j = 1; j <= k; j ++ ){
cin >> num[j];
}
clr ( ls, 0 );
clr ( rs, 0 );
clr ( ss, 0 );
for ( int p = 1; p <= k; p ++ ){
ls[p] = ls[p-1] + num[p];
}
for ( int p = k , p1 = 1; p > 0; p --, p1 ++ ){
rs[p1] = rs[p1-1] + num[p];
}
for ( int p = 1; p <= k; p ++ ){
ss[p] = max ( ss[p], rs[p] );
ss[p] = max ( ss[p], ls[p] );
}
for ( int a = 1; a <= k; a ++ ){
for ( int b = 1; b <= k; b ++ ){
if ( a + b <= k ){
ss[a+b] = max ( ss[a+b], ls[a] + rs[b] );
}
}
}
clr ( tmp, 0 );
memcpy ( tmp, dp, sizeof( dp ) );
for ( int a = 1; a <= k; a ++ ){
for ( int p = m; p >= a; p -- ){
dp[p] = max ( dp[p], tmp[p-a] + ss[a] );
}
}
}
cout << dp[m] << endl;
}
return 0;
}