完美世界的编程题,,大意是小明和boss从一个数列中交替抽取一个数,但是只能从前端和尾段选取。。怎么选取才能使小明和boss抽取的数字之和最大。。贪心不行,要用dp做。
#include <cstdio>
#include<iostream>
#include <algorithm>
using namespace std;
const int MAXN = 6;
int main()
{
int a[MAXN], dp[MAXN][MAXN], sum[MAXN][MAXN];
int n;
std::cin >> n;
for (int i = 0; i < n; i++)
std::cin >> a[i];
for (int i = 0; i < n; i++)
{
sum[i][i] = a[i];
dp[i][i] = a[i];
}
for (int l = 1; l < n; l++)
for (int i = 0; i < n - l; i++)
{
int j = i + l;
sum[i][j] = sum[i][j - 1] + a[j];
dp[i][j] = max(a[i] + sum[i + 1][j] - dp[i + 1][j], a[j] + sum[i][j - 1] - dp[i][j - 1]);
}
std::cout << dp[0][n-1]<<' ';
std::cout << sum[0][n-1] - dp[0][n-1];
system("pause");
return 0;
}