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\n", 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\n", ans);return 0;
}

  

转载于:https://www.cnblogs.com/zhang-yd/p/7718448.html

hihocoder-Week173--A Game相关推荐

  1. hihoCoder 网络流四·最小路径覆盖

    题面带解释 hihoCoder感觉很好. 网络流的精华就是建图 #include<cstdio> #include<iostream> #include<algorith ...

  2. hihoCoder #1142 : 三分求极值

    #1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一 ...

  3. hihocoder 后缀自动机专题

    一.后缀自动机基本概念的理解 1.首先后缀自动机的状态是由子串的endpos来决定的 子串的endpos是指一个子串可以在原字符串的哪些位置进行匹配, endpos构成的不同集合划分成不同的状态 关于 ...

  4. hihoCoder 1051 补提交卡(贪心,枚举)

    #1051 : 补提交卡 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho给自己定了一个宏伟的目标:连续100天每天坚持在hihoCoder上提交一个程序.100天过 ...

  5. hihoCoder week3 KMP算法

    题目链接 https://hihocoder.com/contest/hiho3/problems kmp算法 #include <bits/stdc++.h> using namespa ...

  6. hihoCoder 第136周 优化延迟(二分答案+手写堆)

    题目1 : 优化延迟 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho编写了一个处理数据包的程序.程序的输入是一个包含N个数据包的序列.每个数据包根据其重要程度不同 ...

  7. hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)...

    #1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...

  8. hihoCoder太阁最新面经算法竞赛18

    比赛链接:http://hihocoder.com/contest/hihointerview27/problems A.Big Plus 模拟水 1 #include <bits/stdc++ ...

  9. hihocoder 1061.Beautiful String

    题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings ...

  10. 网络流三·二分图多重匹配 HihoCoder - 1393

    网络流三·二分图多重匹配 HihoCoder - 1393 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int ma ...

最新文章

  1. 今日看点:“靠脸进站”的刷脸系统究竟是如何实现的
  2. MVC3异常处理的方法
  3. 重启nagios有异常提示Starting nagios:This account is currently not available
  4. Spring Cloud Hoxton正式发布,Spring Boot 2.2.x不再孤单
  5. [ZJOI2017]树状数组
  6. RxJS 系列之二 - Observable 详解
  7. linux系统批量杀掉进程命令,[Linux]使用awk批量杀进程的命令
  8. SpringCloud第十章zuul路由网关
  9. 从JDK源码看Writer
  10. 基于ll库,使用stm32L0系列的内部基准电压来进行ad测量
  11. 网页前端培训(JavaScript)
  12. 如何在Macbook上装windows
  13. TheOpenGroupDPBoK个人认证计划
  14. 更改设置并对计算机自定义,电脑别乱玩 禁用Win8.1自定义设置项
  15. 最小生成树(库鲁斯卡尔算法)
  16. ESP8266入门教程11:连接MQTT服务器
  17. 你还记得吗?这几种超级重要的统计学分布
  18. 收藏下Android手机驱动
  19. Vue实现订单列表【用户端】
  20. (转)贝莱德的“阿拉丁”神灯

热门文章

  1. 使用NPOI库导入导出EXCEL
  2. 软件测试实验1:为三角形问题编写一个Java程序,并用Junit测试程序
  3. 一步一步学Silverlight 2系列(25):综合实例之Live Search
  4. 常见问题摘要(生活篇)
  5. POJ 1521 Entropy
  6. 网页UTF8转换GBK后出现问号?的原因
  7. ios系统python编译器_MacBook如何安装Python编译器-百度经验
  8. c语言10个人 三向成绩,C语言入门学习精华:这样学习C语言最有效
  9. 纸筒制作机器人_5个万圣节小手工,带孩子一起动手制作,简单又有趣!
  10. 【APICloud系列|41】融云单聊及消息的处理的对接与实现