Description
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.)Input
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.Output
For each test case you should print one line of output of the form: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 Output
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.
Source
East Central North America 2005上次比賽寫了個記憶化,這次算是跪了。參照:點擊打開鏈接
題意:第二個人是貪心拿法,只能從兩段拿,問最大的差值。
#include#include #include #include #include using namespace std; const int maxn=1100; int dp[maxn][maxn]; int a[maxn],n; int solve(int x,int y) { if(dp[x][y]!=-1) return dp[x][y]; if(x+1==y) return dp[x][y]=abs(a[x]-a[y]); int sa,sb; if(a[x+1]>=a[y])//第一個人選左端 sa=solve(x+2,y)+a[x]-a[x+1]; else sa=solve(x+1,y-1)+a[x]-a[y]; if(a[x]