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 ...
随机推荐
- Python title() 方法
描述 Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写. 语法 title() 方法语法: S.title() 参数 无. ...
- RPC服务框架dubbo(四):Dubbo中Provider搭建
1.新建Maven Project, 里面只有接口(dubbo-service) 1.1 为什么这么做? RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consume ...
- 点滴积累【C#】---抓取页面中想要的数据
效果: 描述:此功能是抓取外国的一个检测PM2.5的网站.实时读取网站的数据,然后保存到数据库里面.每隔一小时刷新一次. 地址为:http://beijing.usembassy-china.org. ...
- android对话框,checkBox,同一时候在同一个页面上保存数据
package com.example.selectonlyonle; import android.app.Activity; import android.app.AlertDialog; imp ...
- action(三)
CCSize boxSize = CCSizeMake(100.0f, 100.0f); CCLayerColor *box = CCLayerColor::create(ccc4(, , , )); ...
- ThreadPoolExecutor线程池解析与BlockingQueue的三种实现
目的 主要介绍ThreadPoolExecutor的用法,和较浅显的认识,场景的使用方案等等,比较忙碌,如果有错误还请大家指出 ThreadPoolExecutor介绍 ThreadPoolExecu ...
- PowerPoint 2010 设置演讲者模式
- PE框架学习
PE开发基础: 开发平台PowerEngine: 开发新功能: 业务逻辑处理: 1.Transaction:交易 2.Chain:链.责任链 3.Command:命令 4.Template:模板 5. ...
- 数论 - 119. Magic Pairs
Magic Pairs Problem's Link Mean: 已知N.A0.B0,对于给定X.Y,若A0X+B0Y能被N整除,则AX+BY也能被N整除,求所有的A.B.(0<=A.B< ...
- DP - 字符混编
字符混编 Problem's Link ---------------------------------------------------------------------------- Mea ...