Sicily 1176 Two Ends

1176. Two Ends

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.

一. 题意理解
给定一列长度为 n 的数列,两个人从中交替取数,只能取两端的数。第一个人可以采取任意策略取数,第二个人只采取“贪心策略”,即每次选较大的那个数,若一样大选左边的数。选的数的和视为分数,谁的分更大谁就胜利。求在第一个人胜利的情况下,最多赢多少分。

二. 解题思路
假设数组为 num[]。用一个二维数组 dif[n][m]表示num[n]~num[m]的最大负值(第二个人输的分数)。第一个人有两种选法:选择 num[n],则第二个人必定会在 num[n+1]和 num[m]之间选一个;选择 num[m],则第二个人必定会在 num[m-1]和 num[n]之间选择一个。那么,只需要选择每一轮选择负值之和最大的情况,最后就能保证最终负值最大。临界条件为 n>=m。为了避免子问题重复计算,我们将 dif[][]初始化为-1,访问到 dif[n][m]时,若值不为-1,就可直接使用。

三. 代码实现

#include<iostream>
using namespace std;
int num[1001];
int dif[1001][1001];

int lose(int n, int m) {//num[n]~num[m]的最大负值总和
	if (dif[n][m] != -1) return dif[n][m];
	if (n >= m) return 0;
	//选择num[n]
	int dif1 = 0;
	if (num[n + 1] < num[m])
		dif1 = num[n] - num[m] + lose(n + 1, m - 1);
	else dif1 = num[n] - num[n+1] + lose(n + 2, m);
	//选择num[m]
	int dif2 = 0;
	if (num[n] < num[m-1])
		dif2 = num[m] - num[m-1] + lose(n , m - 2);
	else dif2 = num[m] - num[n] + lose(n+1, m-1);
	dif[n][m] = dif1 > dif2 ? dif1 : dif2;
	return dif[n][m];
}

int main() {
	int n;
	int count = 1;
	while (cin >> n&&n!=0) {
		for (int i = 0; i < n; i++) cin >> num[i];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) dif[i][j] = -1;
		}
		cout << "In game " << count << ", the greedy strategy might lose by as many as " << lose(0, n - 1) << " points." << endl;
			count++;
	}
	return 0;
}

四. 总结
本题是典型的动态规划问题。我们需要想清楚所有的情况,每一轮拿前面的或拿后面的,分别对应两种情况。每一轮之后统计分数差,取分差最大的情况。为了避免子问题的重复计算,采用记忆化搜索,以此减少耗时。时间复杂度为:O(n)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值