zoukankan      html  css  js  c++  java
  • hihocoder-Week173--A Game

    hihocoder-Week173--A Game 

    A Game

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.

    Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy. 

    输入

    The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)

    The second line contains N integers A1A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)

    输出

    Output the maximum score Little Ho can get.

    样例输入
    4
    -1 0 100 2
    样例输出
    99

    使用区间dp,

    但是我的这种方法只ac了50%, 应该是dp[i][j][1] = max( dp[i][j][1] ,  min( dp[i+1][j][0] , dp[i][j-1][0]) 

    应该对方的策略不是让我方最少,而是对方也取得最优。

    AC 50% Code: 

    #include <cstdio> 
    #include <cstring> 
    
    #include <iostream> 
    using namespace std; 
    
    const int MAXN = 1000 + 10; 
    
    int n, num[MAXN], dp[MAXN][MAXN][2]; 
    
    
    int main(){
    	freopen("in.txt", "r", stdin); 
    
    	int n; 
    	scanf("%d", &n); 
    
    	for(int i=1; i<=n; ++i){
    		scanf("%d", &num[i]); 
    	} 
    
    	memset(dp, 0, sizeof(dp)); 
    
    	for(int i=1; i<=n; ++i){
    		dp[i][i][0] = num[i]; 
    	}
    
    	for(int i=n; i>=1; --i){
    		for(int j=i; j<=n; ++j){
    			dp[i][j][0] = max( dp[i+1][j][1] + num[i],   dp[i][j][0] ); 
    
    			dp[i][j][0] = max( dp[i][j-1][1] + num[j],   dp[i][j][0] ); 
    			
    			dp[i][j][1] = max( dp[i][j][1], min( dp[i+1][j][0] ,  dp[i][j-1][0] ) ); 
    		}
    	} 
    	int ans = dp[1][n][0]; 
    
    	printf("%d
    ", ans);
    
    	return 0; 
    }
    

      

    所以, 

     dp[i][j] = max( sum(i,j) - dp[i+1][j], sum(i,j) - dp[i][j-1]) 

    双方都在求最优,所以 dp[i][j] 指的是当前下手的选手,可以取得的最优成果。 所以当前状态是依赖于前面的 dp[i][j-1] 和 dp[i+1][j] , 

    AC Code 

    #include <cstdio> 
    #include <cstring> 
    
    #include <iostream> 
    using namespace std; 
    
    const int MAXN = 1000 + 10; 
    
    int n, num[MAXN], sum[MAXN], dp[MAXN][MAXN]; 
    
    int main(){
    
    	int n; 
    	scanf("%d", &n); 
    
        sum[0] = 0; 
    	for(int i=1; i<=n; ++i){
    		scanf("%d", &num[i]); 
    		sum[i] = sum[i-1] + num[i];  
    	} 
    
    	memset(dp, 0, sizeof(dp)); 
    	
    	for(int i=1; i<=n; ++i){
    		dp[i][i] = num[i]; 
    	}
    
    	for(int i=n; i>=1; --i){
    		for(int j=i+1; j<=n; ++j){
    			dp[i][j] = (sum[j] - sum[i-1]) - min( dp[i+1][j], dp[i][j-1] ); 
    		}
    	} 
    
    	int ans = dp[1][n]; 
    
    	printf("%d
    ", ans);
    
    	return 0; 
    }
    

      

  • 相关阅读:
    webpack 关于跨域的配置
    如何使用css变量
    样式重置
    vue+element_ui上传文件,并传递额外参数(自动上传)
    LeetCode-46-全排列
    LeetCode-39-组合总数
    LeetCode-33-搜索旋转排序数组
    LeetCode-207-课程表
    LeetCode-15-三数之和
    LeetCode-盛最多水的容器
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7718448.html
Copyright © 2011-2022 走看看