poj1836——dp,最长上升子序列(lis)

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13767   Accepted: 4450

Description

In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.

Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.

Input

On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).

There are some restrictions: 
• 2 <= n <= 1000 
• the height are floating numbers from the interval [0.5, 2.5]

Output

The only line of output will contain the number of the soldiers who have to get out of the line.

Sample Input

8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4

题意:使队列中出列部分士兵,使剩余士兵身高先单增后单减(单调部分不可相等),求最少的出列士兵数量思路:求两次lis(最长上升子序列),再遍历得出答案;lis:dp(i)=max{dp(j)+1,dp(i)},初始化dp[]={1};

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>using namespace std;const int maxn=1000100;
int n;
double h[maxn];
int dp1[maxn],dp2[maxn];int main()
{while(cin>>n){for(int i=0;i<n;i++){cin>>h[i];dp1[i]=dp2[i]=1;}for(int i=0;i<n;i++){  //求最长上升子序列(lis)for(int j=0;j<i;j++){if(h[j]<h[i]) dp1[i]=max(dp1[j]+1,dp1[i]);}}for(int i=n-1;i>=0;i--){for(int j=n-1;j>i;j--){if(h[j]<h[i]) dp2[i]=max(dp2[j]+1,dp2[i]);}}int ans=0;for(int i=0;i<n-1;i++){for(int j=i+1;j<n;j++){if(dp1[i]+dp2[j]>ans) ans=dp1[i]+dp2[j];}}cout<<n-ans<<endl;}return 0;
}

View Code

当然lis有nlogn的做法

转载于:https://www.cnblogs.com/--560/p/4352315.html

poj1836——dp,最长上升子序列(lis)相关推荐

  1. 最长上升子序列(LIS) nlogn解法

    文章目录 经典DP解法O(n^2) dp+二分法(O(nlogn)) 最长上升子序列LIS:Longest increasing subsequence 题目链接:Leetcode300. 最长递增子 ...

  2. 最长上升子序列(LIS)长度

    转自:http://www.slyar.com/blog/poj-2533-cpp.html POJ 2533 Longest Ordered Subsequence 属于简单的经典的DP,求最长上升 ...

  3. 耐心排序之最长递增子序列(LIS)

    目录 一.问题引入 1.最长递增子序列(LIS) 2.问题分析 3.代码实现 4.问题思考 二.耐心排序 1.基本介绍 2.操作步骤 3.代码实现 三.俄罗斯套娃信封问题 1.题目描述 2.问题分析 ...

  4. 最长上升子序列(LIS),牛客刷题

    目录: 最长上升子序列(LIS) 1.模板(数据较小) 2.模板(数据较大) 牛客刷题 1. 牛客练习赛107A:如见青山 2.牛客小白月赛65A牛牛去购物 3.牛客小白月赛65B牛牛去购物 4.牛客 ...

  5. 最长上升子序列(LIS)长度及其数量

    例题51Nod-1376,一个经典问题,给出一个序列问该序列的LIS以及LIS的数量. 这里我学习了两种解法,思路和代码都是参考这两位大佬的: https://www.cnblogs.com/reve ...

  6. 最长上升子序列(LIS)的求法

    最长上升子序列(LIS) 给定一个长度为N的序列A 满足: 1. 1<=x1< x2< x3<-xk<=N 2. A[x1] < A[x2] < A[x3] ...

  7. 最长上升子序列_动态规划 最长上升子序列LIS

    问题描述 最长上升子序列(LIS): 给定长度为n的序列,从中选中一个子序列,这个子序列需要单调递增,请问最长子序列(LIS)的长度? eg:1,5,2,3,11,7,9 则LIS序列为:1,2,3, ...

  8. 最长上升子序列LIS 动态规划 二分查找算法

    所谓LIS表示最长上升子序列,是面试的时候非常容易考察的问题.对于一个序列h1,h2,...hN,其中的子序列hi1,hi2,...hik,满足hi1<hi2<...<hik,那么这 ...

  9. 求最长上升子序列——LIS的O(nlogn)算法(二分)

    LIS的O(nlogn)算法(二分) 传送门:点我 O(n^2)解法:(n为4w,TLE) memset(dp,1,sizeof(dp)); int ans=-1; for(i=2; i<=n; ...

  10. 【训练题】航线设计 | 使用最长上升子序列(LIS)长度的O(nlogn)算法优化

    [问题描述] 有一个国家被一条河划分为南北两部分,在南岸和北岸总共有N对城镇,每一城镇在对岸都有唯一的友好城镇.任何两个城镇都没有相同的友好城镇.每一对友好城镇都希望有一条航线来往.于是他们向政府提出 ...

最新文章

  1. 正则表达式中模式修正符作用详解(i、g、m、s、x、e)
  2. 女生学python可以做什么_学 Python 都用来干嘛的?
  3. 【工具】模板引擎 Velocity
  4. hadoop常见面试题
  5. 共享两个有用的网页布局表格 【有用】
  6. 虎牙AI基础技术部招聘深度学习/计算机视觉实习生
  7. Jconsole工具和Java VisualVM
  8. 【zookeeper】ZooKeeper 权限管理与Curator增加权限验证
  9. 大数据是如何基于 Flink 进行实时计算的?
  10. hadoop3.1.0集群搭建
  11. oracle if 和,oracle if 和 case语句的使用
  12. DEAP数据库介绍--来自于音乐视频材料诱发得到的脑电数据
  13. 源码分析Netty系列
  14. “朝抵抗力最大的路径上走”
  15. hdu 4190 Distributing Ballot Boxes
  16. 小程序根据不同用户,显示不同tabBar
  17. html页面顶部横条,CSS:页面顶部的精简横幅(如本页中的橙色横幅)
  18. 计算机word画铁路,利用WORD画地图
  19. OpenOCD学习笔记 5-stm32f0x配置文件解析
  20. LTS = Long Term Support:长期支持版本

热门文章

  1. 包是如何通过交换机的
  2. Zephry_安装与移植到Stm32F746g_disoc
  3. 记一次自己在Linux上倒腾Nginx的经历
  4. C# 自定义类型通过实现IFormattable接口,来输出指定的格式和语言文化的字符串(例:DateTime)...
  5. 【Java并发编程】—–“J.U.C”:ArrayBlockingQueue
  6. Material Design控件使用
  7. react native 之setState
  8. mysql 查看死锁和去除死锁
  9. 深入了解Java ClassLoader、Bytecode 、ASM、cglib (I)
  10. Android程序开发初级教程(一)