Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10804   Accepted: 3464

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

Source

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int n,dpl[],dpr[];
double num[]; int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%lf",&num[i]);
dpl[i]=dpr[i]=;
} for(int i=;i<n;i++)
for(int j=;j<i;j++)
if(num[i]>num[j] && dpl[i]<dpl[j]+)
dpl[i]=dpl[j]+;
for(int i=n-;i>=;i--)
for(int j=n-;j>i;j--)
if(num[i]>num[j] && dpr[i]<dpr[j]+)
dpr[i]=dpr[j]+;
int ans=;
for(int i=;i<n;i++)
if(ans<dpl[i]+dpr[i]-)
ans=dpl[i]+dpr[i]-;
for(int i=;i<n;i++) //注意新队列中间的两个身高是否相同,相同则不需要减一
for(int j=i+;j<n;j++)
if(num[i]==num[j] && ans<dpl[i]+dpr[j])
ans=dpl[i]+dpr[j];
printf("%d\n",n-ans);
}
return ;
}

POJ 1836 Alignment (双向DP)的更多相关文章

  1. POJ 1836 Alignment 水DP

    题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...

  2. POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14486   Accepted: 4695 Descri ...

  3. poj 1836 Alignment(dp)

    题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...

  4. poj 1836 Alignment(线性dp)

    题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...

  5. POJ 1836 Alignment

    Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11450 Accepted: 3647 Descriptio ...

  6. POJ 1836 Alignment 最长递增子序列(LIS)的变形

    大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 ...

  7. POJ 1836 Alignment --LIS&LDS

    题意:n个士兵站成一排,求去掉最少的人数,使剩下的这排士兵的身高形成“峰形”分布,即求前面部分的LIS加上后面部分的LDS的最大值. 做法:分别求出LIS和LDS,枚举中点,求LIS+LDS的最大值. ...

  8. POJ - 1836 Alignment (动态规划)

    https://vjudge.net/problem/POJ-1836 题意 求最少删除的数,使序列中任意一个位置的数的某一边都是递减的. 分析 任意一个位置的数的某一边都是递减的,就是说对于数h[i ...

  9. poj 1836 LIS变形

    题目链接http://poj.org/problem?id=1836 Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submiss ...

随机推荐

  1. 使用supervisor过程的坑

    1.安装:由于使用的是公司的虚拟机,所以使用pip install supervisor的过程遇到很多权限问题. 中间尝试使用sudo pip install supervisor的方式安装,但是使用 ...

  2. 定义和使用EL函数

    EL为表达式语言,在EL中,允许定义和使用函数.下面将介绍如何定义和使用EL的函数. 1. 定义和使用函数 函数的定义和使用分为以下3个步骤: (1)编写一个Java类,并在该类中编写公用的静态方法, ...

  3. Redis:ERR operation not permitted

    Redis服务开启之后: D:\Redis\redis>redis-server.exe redis.conf 那么另外开一个命令窗口 D:\Redis\redis>redis-cli.e ...

  4. 我所遭遇过的游戏中间件--Apex

    我所遭遇过的游戏中间件--Apex Apex是PhysX的扩展中间件,它是在PhysX的基础上封装了一层.用于实现布料,粒子,破碎这三种物理效果.我只研究其布料处理.使用Apex做物理最大的好处是:它 ...

  5. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  6. Android动画-帧动画

    Android 平台提供了两种动画一种是 Frame动画,即顺序的播放事先做好的图像,与gif图片或者说跟放电影的原理相似,另一种是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果( ...

  7. java读取某个目录是否有新增文件(轮询)

    使用 commons-io 中的文件监控 jar包: package org.demo.file; import java.util.concurrent.TimeUnit; import org.a ...

  8. Cognos利用DMR与文本对象设计中国式报表

    场景:详细对于接触Cognos的人来说,简单的拖拉报表.交叉表这个工作大家都可以完成,下面我们就来实现一下类似下面的效果. 下面简单的说一下实现过程: 利用Framework Manager创建DMR ...

  9. 1-2Html与CSS的关系

    Html和CSS的关系 基于微信的后台开发须要掌握的基础知识包括有:HTML.CSS.JavaScript语言. 以下我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容 ...

  10. java 正则表达式获得html字符串中<img src>中的src中的url地址

    public static Set<String> getImgStr(String htmlStr) { Set<String> pics = new HashSet< ...