Gym 100650H Two Ends DFS+记忆化搜索
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=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //*************************************** int dp[1004][1004]; int n,a[1001]; int dfs(int l,int r) { if(dp[l][r]!=0){return dp[l][r];} if(r==l+1){ return dp[l][r]=abs(a[l]-a[r]); } else { int ans1,ans2; if(a[l+1]>=a[r]) ans1=dfs(l+2,r)+a[l]-a[l+1]; else ans1=dfs(l+1,r-1)+a[l]-a[r]; if(a[l]>=a[r-1]) ans2=dfs(l+1,r-1)+a[r]-a[l]; else ans2=dfs(l,r-2)+a[r]-a[r-1]; return dp[l][r]=max(ans1,ans2); } } int main() { int oo=1; while(scanf("%d",&n)!=EOF) {if(n==0)break; memset(dp,0,sizeof(dp)); int sum=0; for(int i=1;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(1,n)); /// cout<<ans-sum+ans<<endl; } return 0; }