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 ...
随机推荐
- RMAN Recovery Catalog
RMAN用来存放数据库元数据的schema. the catalog includes the following types of metadata:-Data file and archived ...
- 自增长主键Id的设计
http://www.cnblogs.com/lhking/p/3945865.html
- SQLServer 2008中SQL增强之三 Merge(在一条语句中使用
SQLServer 2008中SQL增强之三 Merge(在一条语句中使用Insert,Update,Delete) SQL Server 2008提供了一个增强的SQL命令Merge,用法参看M ...
- 用Jedis操作redis示例一,Key,value HashMap
简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...
- springboot 整合 rabbitmq
http://blog.720ui.com/2017/springboot_06_mq_rabbitmq/
- poj1936
非连续子串匹配题,直接模拟 /** \brief poj 1936 * * \param date 2014/8/5 * \param state AC * \return memory 804k t ...
- $sanitize和$sce服务的使用方法
var app =angular.module(‘myApp‘,[‘ngSanitize‘]); app.controller(‘ctrl‘,function($scope,$sce){ $scope ...
- css 优先级的bug
对于前端而言,了解css样式的优先级,对开发或处理bug有着事半功倍的效果,今天在做项目的时候,突然碰到一个优先级的小问题,刚开始不知道所因,后来才发现这个问题是由优先级造成的.先描述下问题,鼠标悬停 ...
- Dubbo源代码实现三:注册中心Registry
我们知道,对于服务治理框架来说,服务通信(RPC)和服务管理两部分必不可少,而服务管理又分为服务注册.服务发现和服务人工介入,我们来看看Dubbo框架的结构图(来源网络): 图中可以看出,服务提供者P ...
- Unix系统编程()检查进程的存在
检查进程的存在 kill系统调用还有另一重功用.若将参数sig指定为0(即所谓空信号),则无信号发送. 相反,kill仅会去执行错误检查,查看是否可以向目标进程发送信号. 从另一角度来看,这意味着,可 ...