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

      

  • 相关阅读:
    sql server 数据库创建链接服务器
    RabbitMQ消息队列(十四)-启用SSL安全通讯
    RabbitMQ消息队列(十三)-VirtualHost与权限管理
    RabbitMQ消息队列(十二)-性能测试
    RabbitMQ消息队列(十一)-如何实现高可用
    RabbitMQ消息队列(十)-高可用集群部署实战
    centos7 修改yum源为阿里源
    RabbitMQ消息队列(九)-通过Headers模式分发消息(.Net Core版)
    RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
    RabbitMQ消息队列(七)-通过fanout模式将消息推送到多个Queue中(.Net Core版)
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7718448.html
Copyright © 2011-2022 走看看