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. iOS程序间调用

    1.在被调用应用内的info.plist里面设置如下: 鼠标右击information property list ,然后从列表中选择URL types 右击 add row 添加一个对象(item) ...

  2. 没有什么好神秘的: wait_on_page_bit

    文件系统中经常会有wait_on_page_bit函数的封装,比如f2fs中就会有如下的代码: 1431 void f2fs_wait_on_page_writeback(struct page *p ...

  3. C语言 结构体中属性的偏移量计算

    //计算结构体偏移量 #include<stdio.h> #include<stdlib.h> #include<string.h> //详解:对于offscfof ...

  4. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...

  5. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  6. iOS如何上传代码到Github

    iOS如何上传代码到Github 很多iOS开发者想开源自己的代码或者demo,开源到Github是个不错的选择,那么如何上传我们的代码到Github,令所有人可以下载使用呢?这里我们的目的很明确,就 ...

  7. iOS - 语音云通讯

    iOS SDK 2.0 语音及图片消息详解本文档将详细介绍融云的语音及图片消息接口功能及使用说明.阅读本文前,我们假设您已经阅读了融云 iOS 开发指南,并掌握融云 SDK 的基本用法. 语音消息用来 ...

  8. 解决The current branch is not configured for pull No value for key branch.master.merge found in confi

    1.在本地工程目录找到config文件(我的是在E:\rocket\rocket\.git): 2.修改config文件内容为: [core] repositoryformatversion = fi ...

  9. 使用OneNote的COM组件,实现OCR功能。

    背景 在业务系统开发的过程中,很多情况下会去识别图片中的相关信息,并且把信息录入到系统中.现在希望通过自动化的方式录入,就有了以下的工作.在对比了几个OCR软件在中文识别方面的准确率后,决定使用微软的 ...

  10. 免费的SSL证书,免费为微信小程序搭建https

    StartSSL 也免费提供了一个证书(纯英文) 申请地址:https://www.startssl.com/Account?r=L1ZhbGlkYXRl 阿里云免费提供的证书 https://com ...