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. 微软重制Windows 1.0系统:祖师爷出山了

    Windows官方推特在7月1日发布了一条很有趣的动态,“向大家介绍全新的Windows 1.0,带MS-DOS.时钟等”.配发的视频回顾了从Windows 1.0/3.1到Windows 10期间, ...

  2. SQL语句利用日志写shell拿权限

    outfile被禁止,或者写入文件被拦截: 在数据库中操作如下:(必须是root权限) show variables like '%general%'; #查看配置 set global genera ...

  3. JDK8中的HashMap实现原理及源码分析

    大纲 一.什么是Hash?什么是HashMap? 二.HashMap的内部实现机制 1.HashMap基本元素 ①DEFAULT_INITIAL_CAPACITY&MAXIMUM_CAPACI ...

  4. PowerShell中执行.net类库

    Powershell脚本一个比较强大的功能是可以直接调用.net类库(ps core能调用.net core类库),除了调用系统类库外,也可以调用自己编写的类库,从而扩充我们脚本的功能.本文这里简单的 ...

  5. 留学生Essay写作清晰简洁8大原则

    英语Essay写作中国留学生需要掌握的一个技能,一篇文笔简洁优雅的Essay对于提高分数会有很大帮助.但目前的情况是,很多人并没有受过专门的学术Essay写作训练,在写Essay时经常会出现各种各样的 ...

  6. 04 MySQL数据类型

    MySQL支持的数据类型   1.数值类型:       a.整数类型    TINYINT 1字节 有符号(-128~127); 无符号(0~255) SMALLINT 2字节  有符号(-3276 ...

  7. MYSQL 免安装版修改root密码

    c:>mysql –uroot; mysql>show databases; mysql>use mysql;//不用修改 mysql>UPDATE user SET pass ...

  8. 使用NlohmannJson写JSON保留插入顺序

    1. 正文 nlohmann/json是一个C++的读写JSON的组件,号称使用现代C++范式写的.简单看了一下,这个组件确实包含了很多cpp11以上的特性,在vs2015及一下的版本甚至没办法正常编 ...

  9. [Updating]点分治学习笔记

    Upd \(2020/2/15\),又补了一题 LuoguP2664 树上游戏 \(2020/2/14\),补了一道例题 LuoguP3085 [USACO13OPEN]阴和阳Yin and Yang ...

  10. Docker每次启动容器,IP及hosts指定

    原文链接:https://blog.csdn.net/u012834750/article/details/80508464 前言 每次在使用Docker启动Hadoop集群的时候,都需要重新绑定下网 ...