简单的DP,四年前做过的吧
小明过生日的时候,爸爸送给他一副乌龟棋当作礼物。
乌龟棋的棋盘是一行N 个格子,每个格子上一个分数(非负整数)。棋盘第1 格是唯一的起点,第N 格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点。
1 2 3 4 5 ……N
乌龟棋中有M张爬行卡片,分成4种不同的类型(M张卡片中不一定包含所有4种类型 的卡片,见样例),每种类型的卡片上分别标有1、2、3、4四个数字之一,表示使用这种卡片后,乌龟棋子将向前爬行相应的格子数。游戏中,玩家每次需要从所有的爬行卡片中选择 一张之前没有使用过的爬行卡片,控制乌龟棋子前进相应的格子数,每张卡片只能使用一次。
游戏中,乌龟棋子自动获得起点格子的分数,并且在后续的爬行中每到达一个格子,就得到 该格子相应的分数。玩家最终游戏得分就是乌龟棋子从起点到终点过程中到过的所有格子的 分数总和。 很明显,用不同的爬行卡片使用顺序会使得最终游戏的得分不同,小明想要找到一种卡 片使用顺序使得最终游戏得分最多。 现在,告诉你棋盘上每个格子的分数和所有的爬行卡片,你能告诉小明,他最多能得到 多少分吗?
输出只有1 行,1 个整数,表示小明最多能得到的分数
9 5 6 10 14 2 8 8 18 5 17 1 3 1 2 1
73
28116KB
|
100ms
|
/*
* Author: NICK WONG
* Created Time: 8/22/2014 23:40:09
* File Name: tmp.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=1010;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
int f[51][51][51][51],a[maxn],b[maxn],n,m,x;
void init()
{
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=m; i++)
{
cin>>x;
b[x]++;
}
}
void work()
{
for (int i=0; i<=b[1]; i++)
for (int j=0; j<=b[2]; j++)
for (int k=0; k<=b[3]; k++)
for (int l=0; l<=b[4]; l++)
{
int w=1+i*1+j*2+k*3+l*4;
if (i>0) f[i][j][k][l]=max(f[i][j][k][l],f[i-1][j][k][l]);
if (j>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j-1][k][l]);
if (k>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k-1][l]);
if (l>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k][l-1]);
f[i][j][k][l]+=a[w];
}
cout<<f[b[1]][b[2]][b[3]][b[4]]<<endl;
}
int main()
{
init();
work();
return 0;
}