码迷,mamicode.com
首页 > 其他好文 > 详细

sicily 1176 two ends 动态规划解题

时间:2014-09-29 04:25:56      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   ar   for   sp   div   art   

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 <iostream>
#include <string.h>
using namespace std;

int point1_dp(int a, int b);
int nums[1010];
int dp[1010][1010];

int main() {
	int n;
	int num = 1;
	while (cin >> n && n) {
		memset(nums, 0, sizeof(nums));
		memset(dp, -1, sizeof(dp));
		int sum = 0;
		int value = 0;
		for (int i = 1; i <= n; i++) {
			cin >> value;
			nums[i] = value;
			sum += value;
		}
		int point1 = point1_dp(1, n);
		int point2 = sum - point1;
		cout << "In game " << num++ << ", the greedy strategy might lose by as many as " << point1 - point2 << " points." << endl;
	}
	return 0;
} 

//该函数为带缓存的自顶向下的动态规划方法 
int point1_dp(int a, int b) {
	int start_l = 0, start_r = 0;
	if (b - a == 1) {
		if (nums[a] > nums[b]) {
			return dp[a][b] = nums[a];
		} else {
			return dp[a][b] = nums[b];
		}
	}
	//未加这个会超时 ,即每次一旦求得dp[a][b]的值,则直接返回而不用继续算下去了 
	if (dp[a][b] != -1) {
		return dp[a][b];
	}
	//从左开始
	//若用 > 会wrong answer ,因为规定当相等时,优先从左选 
	if (nums[a+1] >= nums[b]) {
		start_l += nums[a] + point1_dp(a+2, b);
	} else {
		start_l += nums[a] + point1_dp(a+1, b-1);
	}
	//从右开始
	if (nums[a] >= nums[b-1]) {
		start_r += nums[b] + point1_dp(a+1, b-1);
	} else {
		start_r += nums[b] + point1_dp(a, b-2); 
	}
	
	//动态规划求最优解
	if (start_l > start_r) {
		dp[a][b] = start_l;
	} else {
		dp[a][b] = start_r;
	}
	return dp[a][b]; 
}

  

sicily 1176 two ends 动态规划解题

标签:des   blog   io   os   ar   for   sp   div   art   

原文地址:http://www.cnblogs.com/xieyizun-sysu-programmer/p/dp.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!