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. Helm 架构【转】

    在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...

  2. idea跑mapreduce结果为空白文本,idea代码被莫名其妙地改动了

    遇到如题的错误, 一开始查找Step1Main.java的代码错误,尝试关掉分区设置,还是一样. 后来以为是mapper或reducer不执行,网上查找了半天也没有正确原因. 最终,偶然间看到redu ...

  3. linux7 安装Docker

    Docker:用白话文简单介绍就是一个集装箱,可以将其运行环境及依赖打包,方便各种场合使用.Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机 ...

  4. Intellij IDEA中配置TFS

    TFS是微软推出的一款研发过程管理利器,C#阵营的VS里做了默认集成,但是对于Java阵营的Intellij IDEA,需要安装插件并进行相应配置才能使用: 1.打开配置 2.搜索并安装插件 3.配置 ...

  5. Netty 异步模型

    简介 Netty中的 I/O 操作是异步的, 包括 Bind.Write.Connect 等操作会简单的返回一个ChannelFuture. 调用者不能立刻获得结果, 而是通过Future-Liste ...

  6. 小技巧:使用命令行打开vscode 以及 sublime 工具

    vscode手动打开vscode command + shift + p 打开命令面板(或者点击菜单栏 查看>命令面板)输入 shell 选择 install code command in P ...

  7. redmine处理规范

         开发: 1.       研发人员负责更新到的状态共有三个:  “进行中”. ”已解决”. ”需要反馈”. 2.       在开始修复bug的时候,把状态更新为”进行中”,把title更新 ...

  8. 安装npm install时,长时间停留在fetchMetadata: sill 解决方法——换npm的源

    安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...

  9. python 输出99乘法表

    for i in range(1,10): for j in range(1,i+1): print("%s*%s=%2s"%(i,j,i*j),end=" " ...

  10. ssh: connect to host 120.79.26.164 port 22: Connection timed out报错问题

    要是使用阿里云服务器,出现这种错误,一般是端口没有打开.需要在阿里云控制台中设置端口后,即可使用ssh连接.