Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15135   Accepted: 4911

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

题意:一排人排队,要保证向前左或向右看到无穷远处,
从左找最长递增序列,从右找最长递增序列,然后枚举i,求 i 后面的<= a[i]中最大的那个
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = + ;
const double F = 10e-;
double heigh[MAX];
int dp1[MAX],dp2[MAX];
void LCA(int n)
{
memset(dp1, , sizeof(dp1));
dp1[] = ;
for(int i = ; i <= n; i++)
{
int maxn = ;
for(int j = i - ; j > ; j--)
{
if(heigh[i] > heigh[j])
{
maxn = max(maxn,dp1[j]);
}
}
dp1[i] = max(dp1[i], maxn + );
}
}
void LDA(int n)
{
memset(dp2, , sizeof(dp2));
dp2[n] = ;
for(int i = n; i > ; i--)
{
int maxn = ;
for(int j = i + ; j <= n; j++)
{
if(heigh[i] > heigh[j])
{
maxn = max(maxn, dp2[j]);
}
}
dp2[i] = max(dp2[i], maxn + );
}
}
int Cout(int n)
{
int maxn = ;
for(int i = ; i <= n; i++)
{
int temp = ;
for(int j = i + ; j <= n; j++)
{
if(heigh[i] >= heigh[j])
temp = max(temp, dp2[j]);
}
maxn = max(maxn, dp1[i] + temp);
}
return n - maxn;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= n; i++)
{
scanf("%lf", &heigh[i]);
}
LCA(n);
LDA(n);
printf("%d\n",Cout(n));
}
return ;
}

POJ1836Alignment(LCA)的更多相关文章

  1. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  2. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

  3. 面试题6:二叉树最近公共节点(LCA)《leetcode236》

    Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...

  4. P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  5. 洛谷P3379 【模板】最近公共祖先(LCA)(dfs序+倍增)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  6. 「LuoguP3379」 【模板】最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  7. 洛谷——P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  8. luogo p3379 【模板】最近公共祖先(LCA)

    [模板]最近公共祖先(LCA) 题意 给一个树,然后多次询问(a,b)的LCA 模板(主要参考一些大佬的模板) #include<bits/stdc++.h> //自己的2点:树的邻接链表 ...

  9. 【原创】洛谷 LUOGU P3379 【模板】最近公共祖先(LCA) -> 倍增

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

随机推荐

  1. smarty中三种变量的访问方式

    在模板中smarty有三种变量,第一种,php分配的变量,第二种配置文件里的变量,第三种,PHP全局数组里的变量,配置文件里变量的访问方式可以是{#bgcolor#},"#"必须紧 ...

  2. Pureftp-安全的ftp服务器部署

    一.简介: Pure-FTPd 是一款免费(BSD)的,安全的,高质量和符合标准的FTP服务器. 侧重于运行效率和易用性. 它提供了简单的答案,他满足了大众化的需求,包括普通用户以及主机供应商们 Pu ...

  3. 12SpringMvc_在业务控制方法中写入普通变量收集参数

    这篇文章讲的是jsp页面不是会传一些参数到Action中,那么Action怎么去接受这个数据呢? 方案: 案例结构如下:

  4. RDLC直接打印帮助类

    代码 /// <summary> /// 打印帮助类 /// </summary> public class PrintHelper { private int m_curre ...

  5. [转]一个四叉树Demo学习

    程序代码: http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 四叉树: using Sys ...

  6. [iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(下)

    三. 创建文件夹 问题: 你想创建文件夹到磁盘,存储一些文件到里面 解决方案: 使NSFileManager类的实例方法createDirectoryAtPath:withIntermediateDi ...

  7. 传智播客C++第五期培训视频教程免费下载

    C/C++的应用领域几乎无处不在,服务器,嵌入式,物联网,移动互联网,信息安全,游戏,基本上大小通吃.C/C++市场份额高达26%,也就是每四个程序员就有一个C/C++程序员.市场需求量非常大,而且工 ...

  8. Python类库下载

    https://sourceforge.net/projects/pywin32/files/pywin32/ WMI库的安装 下载 http://timgolden.me.uk/python/wmi ...

  9. 【MPI学习2】MPI并行程序设计模式:对等模式 & 主从模式

    这里的内容主要是都志辉老师<高性能计算之并行编程技术——MPI并行程序设计> 书上有一些代码是FORTAN的,我在学习的过程中,将其都转换成C的代码,便于统一记录. 这章内容分为两个部分: ...

  10. 【android】实现一个自己的标题栏

    完整项目下载 背景:项目中使用标题栏,只是简单的include一个标题栏的视图,赋值.控制元素显示.点击事件都要自己搞,不优雅! 要求: 1:对现有代码入侵最小 2:使用足够简单 OK,围绕着这个需求 ...