您的位置:首页 > 其它

sicily 1176. Two Ends

2015-09-24 16:05 363 查看

1176. Two Ends

Constraints

Time Limit: 1 secs, Memory Limit: 64 MB

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.)

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.

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:

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 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.


#include
#include
using namespace std;

#define MAX 1000000

int arr[1001];
int maxdiff[1001][1001];

int dp(int begin, int end) {
	if (begin + 1 == end) {
		return abs(arr[begin] - arr[end]);
	}
	else if (maxdiff[begin][end] != MAX) {
		return maxdiff[begin][end];
	}
	else {
		int t1, t2;
		// 假定第一个人选择左边(假定为左边是最优)。第二个人的选择分两种情况。然后递推if
		if (arr[begin + 1] >= arr[end]) {
			t1 = arr[begin] - arr[begin + 1] + dp(begin + 2, end);
		}
		// 假定第一个人选择右边(假定为右边是最优)。同上
		else {
			t1 = arr[begin] - arr[end] + dp(begin + 1, end - 1);
		}
		
		if (arr[begin] >= arr[end - 1]) {
			t2 = arr[end] - arr[begin] + dp(begin + 1, end - 1);
		}
		else {
			t2 = arr[end] - arr[end - 1] + dp(begin, end - 2);
		}
		// 比较选左边和选右边哪个最优
		maxdiff[begin][end] = t1 > t2 ? t1 : t2;
		return maxdiff[begin][end];
	}
}

int main() {
	int n, count = 0;;
	cin >> n;
	while (n != 0) {
		count++;
		for (int i = 0; i < n; i++) cin >> arr[i];
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++)
				maxdiff[i][j] = MAX;
		}
		cout << "In game " << count << ", the greedy strategy might lose by as many as "
			<< dp(0, n - 1) << " points." << endl;
		cin >> n;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: