Gym 100650H Two Ends DFS+记忆化搜索

时间:2024-09-21 22:06:38

Problem H: Two Ends
In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face
up, is written a positive integer. Players take turns removing a card from either end of the row and
placing the card in their pile. The player whose cards add up to the highest number wins the game.
Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy
strategy. However, this is not always optimal, as the following example shows: (The first player would
win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player
uses it but the first player is free to use any strategy she wishes.
InputOutput
There will be multiple test cases. Each test case will be contained on one line. Each line will start with
an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may
assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in
the list does not exceed 1,000,000.
OutputSample Input
For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference
between the first player’s score and second player’s score when the second player uses the greedy strategy.
When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.
Sample Input
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0
Sample ouput
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

题意:

给你一个序列,A和B轮流可以来选掉序列两端的一个值,B总是选两个里面最大的,而A按最佳方案选;问你AB的最大分差是多少;

题解:

dp[l][r]记录l->r的最大分差,进行DFS暴力

代码

//зїеп:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************
int dp[][];
int n,a[];
int dfs(int l,int r)
{
if(dp[l][r]!=){return dp[l][r];}
if(r==l+){
return dp[l][r]=abs(a[l]-a[r]);
}
else {
int ans1,ans2;
if(a[l+]>=a[r])
ans1=dfs(l+,r)+a[l]-a[l+];
else ans1=dfs(l+,r-)+a[l]-a[r];
if(a[l]>=a[r-])
ans2=dfs(l+,r-)+a[r]-a[l];
else ans2=dfs(l,r-)+a[r]-a[r-];
return dp[l][r]=max(ans1,ans2);
} }
int main()
{
int oo=;
while(scanf("%d",&n)!=EOF)
{if(n==)break;
memset(dp,,sizeof(dp));
int sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
} printf("In game %d, the greedy strategy might lose by as many as %d points.\n",oo++,dfs(,n));
/// cout<<ans-sum+ans<<endl;
}
return ;
}