zoukankan      html  css  js  c++  java
  • sicily 1176 two ends 动态规划解题

    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]; 
    }
    

      

  • 相关阅读:
    【Matlab】把一年中的某一天(从1月1日起)换算成日期
    【工具】用hexo搭建博客
    【工具】文献分析工具histcite的简单使用
    【工具】用PPT排版打印海报时图片分辨率问题
    【工具】PPT插入高清图片保存后图片变模糊的解决方法
    【工具】排版软件TeX Live 2016的简单使用
    【工具】文字识别软件(OCR) ABBYY Finereader 11简单使用
    【Matlab】编程风格摘录
    【信号】用matlab实现一维信号的高斯滤波
    【GMT5】用GMT绘制测高卫星Topex_Poseidon_Jason的地面轨迹
  • 原文地址:https://www.cnblogs.com/xieyizun-sysu-programmer/p/dp.html
Copyright © 2011-2022 走看看