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. 2018.12.26 考试(哈希,二分,状压dp)

    T1 传送门 解题思路 发现有一个限制是每个字母都必须相等,那么就可以转化成首尾的差值相等,然后就可以求出\(k-1\)位的差值\(hash\)一下.\(k\)为字符集大小,时间复杂度为\(O(nk) ...

  2. 点击手机返回键弹出Dialog对话框功能

    在程序中,我们为了防止出现客户在使用程序填信息或者浏览页面时因误点返回键造成关闭界面的现象,需要添加弹出框功能,以确认客户是否要退出本界面,下面是功能实现的代码: 1.点击手机返回键的判断 publi ...

  3. Codeforces ~ 1009C ~ Annoying Present (贪心)

    题意 一个长度为n的数组(初始全为0),进行m次操作. 操作:给你x,d,你任意挑选一个 i (1~n),每个数字加上 x+|i-j|*d( j 表示对应数字的下标) 问m次操作后的最大算术平均值为多 ...

  4. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

  5. jmter 5.1 中文

    一.jmeter5.0下载解压后,默认的界面是英文版的,许多人觉得不方便,想要汉化,jmeter是不需要安装汉化包的,通过修改配置文件即可:1.找到jmeter解压后的文件夹,例如我是安装在D:\De ...

  6. prototype.原型链.原型链图

      //1.几乎所有函数都有prototype属性,这个是个指针,指向原型对象;Function.prototype这个没有 //2.所有对象中都有__proto__属性.(Object.protot ...

  7. PAT_A1025#PAT Ranking

    Source: PAT A1025 PAT Ranking Description: Programming Ability Test (PAT) is organized by the Colleg ...

  8. Java并发主要操作

    核心Java提供对多线程程序的完全控制. 也可以开发一个可以根据您的要求完全暂停,恢复或停止的多线程程序. 有各种静态方法可以用于线程对象来控制它们的行为. 下表列出了这些方法 - 编号 方法 说明描 ...

  9. wordpress 上传图片时提示“无法建立目录wp-content/uploads/2019/03。有没有上级目录的写权限?”

    查一下网站目录下wp-content目录的权限, # ls -l drwxr-xr-x  5 nobody 65534  4096 Feb  3  2016 wp-content 修改wp-conte ...

  10. 十、 shell基础

    shell的表现形式: history -c 清空历史命令(清空缓存;默认:1000条) -w 将缓存中的历史命令保存到配置文件中 ~/.bash_history 永久保存历史命令(默认:1000条) ...