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

题意:
  给出一列士兵的身高(会有重复),要求求出剔除最少士兵使得每一个士兵往左或者右看可以看到该队列的尽头士兵,原有的位置不得改变。
   注意:
  1. 不能直接排序去剔除除去最高士兵以外有重复的元素,因为题目要求原来的队列顺序不变。
  2. 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.也就是说在最后得到的队列中,除了最高的那个士兵可以有重复身高之外,其他的士兵的身高不允许有重复。(这里需要纠正一下,最高的身高不允许有重复,不仅如此,所有的身高都不允许有重复,否则往左或者往右看都看不过去,会被等身高的人挡住)。即最多有一个极值。

思路: 

  相当于给一列数num[n],要求求删掉最少的个数,任取其中一个元素num[i],满足:1.num[0]~num[i]单调递增;2.num[i]~num[n-1]单调递减。即最多有一个极值。

  求出一个LIS一个LDS(需要倒着扫),然后从队首到队尾枚举位置,最后ans=n−max(dp1[i]+dp2[i])。

 原数据:1.86  1.86  1.30621  2  1.4  1  1.97  2.2
 最长上升子序列:1.86
         1.30621 2
        1.30621 1.4 (1.4去替换掉了2的位置)
         1 1.4 1.97 2.2 (1比1.30621更优,去替换掉了1.30621的位置,而后面的数据不需要更新)(最后凭借这个序列求出最长长度,这个的最长长度是和正确排序得出的长度是一样的)
        (1.30621 1.4 1.97 2.2 )(而这个长度与上面那一组数据长度一样,这个是正确排序的长度)
         最长长度:4
 最长下降子序列:1.86 1.30621
         2 1.30621
         2 1.4
         2 1.4 1
         2 1.97 1
         2.2 1.97 1
         (1.86 1.30621 1;1.86 1.4 1;2 1.4 1)
         最长长度:3
(需要注意的是:求出来的只是序列的最长上升或下降长度,但是里面的元素不一定是按照上升或者下降的顺序进行排好的,只是用一个更优的数据去替换掉了原有的数据的位置。) 针对该题时间复杂度问题:对于最长上升子序列问题有两种写法,正常写O(n2),怕超时可以用O(nlogn)的写法。
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; float a[];
int dpup[];
int dpdown[]; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin>>n;
memset(dpup,,sizeof(dpup));
memset(dpdown,,sizeof(dpdown));
for(int i=; i<n; i++)
cin>>a[i];
for(int i=; i<n; i++)
{
dpup[i]=;
for(int j=; j<i; j++)
{
if(a[j]<a[i])
{
dpup[i]=max(dpup[i],dpup[j]+);
}
}
}
//
// for(int i=0; i<n; i++)
// {
// dpdown[i]=1;
// for(int j=0; j<i; j++)
// {
// if(a[j]>a[i])
// {
// dpdown[i]=max(dpdown[i],dpdown[j]+1);
// }
// }
//
//这两个上升和下降序列单纯判断是没有问题的,但是在这一题中,这样判断会出现交叉的情况,所以不能这样子写 for(int i=n-; i>=; i--) //这里需要倒着扫
{
dpdown[i]=;
for(int j=n-; j>i; j--)
{
if(a[i]>a[j])
{
dpdown[i]=max(dpdown[j]+,dpdown[i]);
}
}
}
int ans=-inf;
for(int i=; i<n; i++)
{
for(int j=i+; j<n; j++)
{
// if(a[i]==a[j])
// ans=max(ans,dpup[i]+dpdown[j]-1);
// else
ans=max(ans,dpup[i]+dpdown[j]);
}
}
//这里的第二层循环从i+1开始,已经间接排除掉了顶峰会出现两个相等的情况
//不存在顶峰出现三个甚至更多的情况,因为之前求得时候一个单调递增,一个单调递减,要是重复只会重复一次
cout<<n-ans<<endl;
return ;
}

  

  

POJ-1836-Alignment-双向LIS(最长上升子序列)(LIS+LSD)+dp的更多相关文章

  1. POJ - 1631 Bridging signals(最长上升子序列---LIS)

    题意:左右各n个端口,已知n组线路,要求切除最少的线路,使剩下的线路各不相交,按照左端口递增的顺序输入. 分析: 1.设左端口为l,右端口为r,因为左端口递增输入,l[i] < l[j](i & ...

  2. poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

  3. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...

  5. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  6. POJ 1631 Bridging signals (LIS:最长上升子序列)

    题意:给你一个长为n(n<=40000)的整数序列, 要你求出该序列的最长上升子序列LIS. 思路:要求(nlogn)解法 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序 ...

  7. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  8. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  9. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  10. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

随机推荐

  1. C++一些不常见的库及函数

    pbds库 平衡树:one , two #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; ...

  2. IDEA上传项目到SVN

    1.打开IDEA ,上面工具栏选择VCS 选择把项目交给SVN管理 2.选择SVN 3.选择SVN管理后可以看到项目变这个颜色 4.右键项目选择如下 5.点击绿色的+号,选择一个SVN仓库的地址,下面 ...

  3. 广度优先搜索(Breadth First Search)

    Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...

  4. 【vue】---vue中使用async+await出现的问题及解决方案

    一.在Vue中出现的问题 因为我没有用脚手架,自己用webpack配置的环境,因此报了以下错误,出现的问题应该是缺少解析器的原因 二.解决方案 安装: npm i babel-plugin-trans ...

  5. python 中 random模块的用法

    import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 print( random.random() ) # 产生 0 到 ...

  6. Unity3D中动态创建编辑轴(点,线,圆,圆锥)

    问题分析: 最近在搞软件底层开发,将一些工具或者底层脚本打成dll导入unity使用,有这样一需求,就是编辑功能,需要像Scene场景一样,实现那种编辑轴 实现方式: 创建Mesh,构建编辑轴,这个地 ...

  7. keepAlived常见问题

    1.脑裂问题 1.定义: 在一个高可用(HA)系统中,当关联着的两个结点互相之间断开通信时,本来为一个独立的对外提供服务的系统分裂为两个独立的结点,这时两个结点会争抢资源.(keepAlived 中表 ...

  8. PHP操作XML方法之SimpleXML

    SimpleXML简介 SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将XML转换成一个带有一般属性选择器和数组迭代器的对象. 举例XML XML结构部分引用自<<深入理 ...

  9. tp6 控制器不存在:app\index\controller\Index

    tp6 控制器不存在:app\index\controller\Index config/app.php 修改如下 'auto_multi_app' => true,

  10. windows 远程登录

    在我的电脑  属性当中开启远程登录功能 然后制作账号,这里对于账号的命名是有要求的,具体网上查找说是要用户名和全名一样,我没有做测试,不过新创建了一个用户用来登录,总是告知我密码错误,导致登录失败,所 ...