Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14492   Accepted: 4698

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

题意是给出了一个序列,希望这个序列满足这个序列中的每一个数,要么是从左到右的最大值,要么是从右到左的最大值。现在不满足,需要从当前序列中抽走几个数重新排能满足上述的条件。

从左到右求一次递增,从右到左求一次递增。求在每一个数之内其从左到右+从右到左 递增序列的最大值。用总和相减即可。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int num;
int l_dp[2000];
int r_dp[2000];
double value[2000]; int main()
{
int i,j,max_v;
scanf("%d",&num); for(i=1;i<=num;i++)
{
cin>>value[i];
l_dp[i]=1;
r_dp[i]=1;
}
max_v=0;
for(i=1;i<=num;i++)
{
max_v=0;
for(j=1;j<i;j++)
{
if(value[i]>value[j])
{
max_v=max(l_dp[j],max_v);
}
}
l_dp[j]=max_v+1;
} for(i=num;i>=1;i--)
{
max_v=0;
for (j = num; j > i; j--)
{
if(value[i]>value[j])
{
max_v=max(r_dp[j],max_v);
}
}
r_dp[j]=max_v+1;
}
for(i=1;i<=num;i++)
{
l_dp[i]=max(l_dp[i],l_dp[i-1]);
}
for(i=num;i>=1;i--)
{
r_dp[i]=max(r_dp[i],r_dp[i+1]);
}
max_v=0;
for(i=1;i<=num;i++)
{
max_v = max(l_dp[i]+r_dp[i+1],max_v);
} cout<<num-max_v<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1836:Alignment的更多相关文章

  1. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  2. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  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 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...

  5. poj 1836 Alignment(线性dp)

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

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

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

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

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

  8. POJ 1836 Alignment --LIS&LDS

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

  9. POJ 1836 Alignment

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

随机推荐

  1. 并行执行 Job【转】

    有时,我们希望能同时运行多个 Pod,提高 Job 的执行效率.这个可以通过 parallelism 设置. 这里我们将并行的 Pod 数量设置为 2,实践一下: Job 一共启动了两个 Pod,而且 ...

  2. R 《回归分析与线性统计模型》page164 单变量、多变量多项式模型

    --多项式回归模型 --单变量多项式模型 --多变量多项式模型 rm(list = ls()) library(openxlsx) library(leaps) #单变量多项式模型# data = r ...

  3. 034、Java中自增之++在前面的写法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. 区块链技术核心概念与原理讲解-Tiny熊

    转载自简书ceido:https://www.jianshu.com/u/fcdf49ef65bb (1)区块链前世今生 密码朋克(Cypherpunk):是一个邮件组,里面有许多大牛. 区块链不是单 ...

  5. JuJu团队11月26号工作汇报

    JuJu团队11月26号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达 对原始文本进行预处理, 并转换成可被julia读入的格式 完成预处理并用julia读入. 读入后按 ...

  6. LeetCode874 模拟行走机器人(简单模拟—Java之HashSet简单应用)

    题目: 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度-1:向右转 90 度1 <= x <= 9 ...

  7. Spring容器的创建原理

    1.new ioc容器(AnnotationConfigApplicationContext 注解ioc) 2.refresh()方法调用 2.1 prepareRefresh()刷新前的预处理 a: ...

  8. python中pip的安装问题

    当系统中同时存在python2.python3的时候,在进行pip下载的时总出现不知道用哪个 情况,此时就需要这样做: python2 -m pip install numpy //使用pip给pyt ...

  9. ffmpeg 多路实时问题之解决思路

     记得前面有人提出多路视频不实时问题,这个首先需要从网络带宽上查看是否视频帧全实时的到达,还有一个问题就是,即使视频帧全部到达,看起CPU也是足够的,但是却表现为慢镜头这种样子,那么很可能是解码显示的 ...

  10. mybatis关于级联查询结果集嵌套映射对象非列表的处理问题

    工作中遇到这么一个问题,嵌套查询,返回json的时候,作为属性,deviceFields是一个device中的一个对象属性,在json返回的时候想要得到的应该是deviceFields:{ 具体属性} ...