poj_1836 动态规划
题目大意
N个士兵排成一排,不是按照高度顺序排列。现在想要从中去掉几名士兵,从而使得队伍中剩余的士兵能够看到这排最左边或者最右边的那个士兵,某士兵能够看到最左边(或最右边)的士兵指这名士兵和最左边(或最右边)士兵之间没有另外一名士兵的高度大于等于这名士兵。
题目分析
典型的最长xx子序列问题,能够满足要求的队列有四种:
(1)严格上升子序列
(2)严格下降子序列
(3)以某个士兵为中心,左边上升,右边下降子序列
(4)以士兵i左边为上升子序列,士兵j右边为下降子序列,且i在j左边。
然后排成上述四种队形,找满足某个队形要求的最大的队列中人数。
实现(c++)
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<set>
#include<map>
#include<functional>
#include<algorithm>
using namespace std;
typedef long long ll;
double height[1005];
int dp_inc[1005]; //从最左边到达i处,以i结尾的最长上升子序列长度
int dp_dec[1005]; //从i到达最右边,以i开头的最长下降子序列长度
int Solve(int n){
for (int i = 0; i < n; i++){
dp_inc[i] = 1;
for (int j = i - 1; j >= 0; j--){
if (height[j] < height[i]){
dp_inc[i] = max(dp_inc[i], dp_inc[j] + 1);
}
}
}
for (int i = n - 1; i >= 0; i--){
dp_dec[i] = 1;
for (int j = i + 1; j < n; j++){
if (height[j] < height[i]){
dp_dec[i] = max(dp_dec[i], dp_dec[j] + 1);
}
}
}
int result = 0;
for (int i = 0; i < n; i++){ //选择左侧上升,右侧下降
for (int j = i; j < n; j++){
result = max(result, dp_inc[i] + dp_dec[j] - (i == j));//如果i和j重合,则去掉重合的一次
}
}
return result;
}
int main(){
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%lf", &height[i]);
}
int result = Solve(n);
printf("%d\n", n - result);
return 0;
}
poj_1836 动态规划的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- 线程池c3p0和dbcp2的配置初始化实例
一.c3p0 public class ConnectionManager { public static ComboPooledDataSource dataSource; static { try ...
- python 2/3 joblib.dump() 和 joblib.load()
在python2中加载python3训练和保存的模型时出错: ValueErrorTraceback (most recent call last) --> 237 clf = joblib.l ...
- ORID方法在敏捷中的利用
Objective: 上个迭代有哪些让你印象深刻的事情发生?你看到了什么? Reflective:哪些场景让你兴奋?哪些地方不那么顺利? Interpretive:为什么会不顺利?这些数据使你意识到了 ...
- mysql数据类型与运算符
一.数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节 范围(-128~127) smallint(m) 2个字节 范围(-32768~32767) mediu ...
- ajax创建
ajax对象创建和使用 //创建ajax对象 function createXMLhttp(){ var xmlhttp; if(window.XMLHttpRequest) {// code for ...
- 大数据学习之Scala中main函数的分析以及基本规则(2)
一.main函数的分析 首先来看我们在上一节最后看到的这个程序,我们先来简单的分析一下.有助于后面的学习 object HelloScala { def main(args: Array[String ...
- CCFollow和ActionCallFunc
CCFollow动作,可以让一个节点跟随另一个节点做位移. CCFollow经常用来设置layer跟随sprite,可以实现类似摄像机跟拍的效果 效果是精灵在地图上移动,地图也会跟着移动,但是精灵仍然 ...
- The difference between Severity and Priority
The difference between Severity and Priority[1] 2015-06-24 There are two key things in defects of th ...
- C++链接ODBC数据源:VS2013,Access
参考资料:1.http://wenku.baidu.com/view/a92d1a812cc58bd63186bd8d.html 2.http://blog.sina.com.cn/s/blog_68 ...
- Linux crontab 实现每秒执行
Linux crontab 实现每秒执行 linux crontab 命令,最小的执行时间是一分钟.如需要在小于一分钟内重复执行,可以有两个方法实现. 1.使用延时来实现每N秒执行 创建一个php做执 ...