Alignment--POJ1836

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

题意是n个士兵站队,最终使得每个士兵的左边或者右边的人的身高都是一次下降的,求的是出队的最小人数即总人数减去最多剩余的人数;可以先求出从左到右的递增子序列和从右到左的递增子序列;

代码如下:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
#define MAXN 1010
double s[MAXN];
int a[MAXN],b[MAXN];
int n;
int main()
{
    while(cin>>n)
    {
        int i,j;
        for(i=1; i<=n; i++)
            scanf("%lf",&s[i]);
        a[1]=1;
        for(i=2; i<=n; i++) //从头开始计算最长的递增序列
        {
            a[i]=1;
            for(j=1; j<i; j++)
            {
                if(s[i]>s[j]&&a[j]>=a[i])
                    a[i]=a[j]+1;
            }
        }
        b[n]=1;
        for(i=n-1; i>=1; i--) //从尾部计算最长的递增序列
        {
            b[i]=1;
            for(j=n; j>i; j--)
            {
                if(s[i]>s[j]&&b[j]>=b[i])
                    b[i]=b[j]+1;
            }
        }
        int maxn=-100000;//初始化max
        for(i=1; i<=n; i++)
        {
            for(j=i+1; j<=n; j++)
            {
                if(a[i]+b[j]>maxn)
                    maxn=a[i]+b[j];
            }
        }
        printf("%d\n",n-maxn);
    }
    return 0;
}
时间: 2024-10-15 18:37:45

Alignment--POJ1836的相关文章

POJ1836 Alignment 【LIS(二分)+枚举】

a1,a2,a3,a4,a5,a6...an 对ai求出a1到ai的lis,ai+1到an的lds 取所有ai对应的lis+lds最大值 输出n-lis-lds #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int

POJ1836:Alignment(LIS的应用)

题目链接:http://poj.org/problem?id=1836 题目要求: 给你n个数,判断最少去掉多少个数,从中间往左是递减的序列,往右是递增的序列 需注意的是中间可能为两个相同的值,如 1 2 3 3 2 1 输出为0 题目分析: 这题和UVA10534极其相似,因为刚做完,就果断粘贴复制了,唯一的不同是如果中间有两个士兵一样高是不用去掉的,(中间,仅限两个). 我感觉这题是水过去的,我的代码只支持有一个最大值的情况,我的做法是先判断,如果求得最大值时这个点在队列当中的位置如果在边界

poj1836——dp,最长上升子序列(lis)

poj1836——dp,最长上升子序列(lis) Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13767   Accepted: 4450 Description In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight

Data alignment漫谈

特此声明:本人CSDN中的所有文章未经授权,不得转载,违者必究!一切被授权人.组织等的信息都会记录在本人CSDN授权文章的列表中. 以下讨论的概念性的东西应该都是适用于所有系统的,但是实际操作都是linux系统做的. 讨论基于单线程处理,目的是为了简化讨论,简化测试,但并不影响对理论的验证. 最后附上验证源码以及其解释. 背景之个人理解 本节内容都是个人的理解,如果有不正确的地方,欢迎讨论. 说内存对齐之前,先说说硬盘对数据的组织方式.我们知道,文件系统管理数据是按照块来管理的,假如说一个4k对

Supervised Descent Method Face Alignment 代码下载 和 算法研究 之一

1 主要内容: Supervised Descent Method and its Applications to Face Alignment算法研究. 2代码彩蛋:我问了好久,xxiong好心人发给我的,希望能对你们学习有帮助: 低调下载: http://humansensing.cs.cmu.edu/xxiong/mexintraface1.3.1%28release%29.zip. 注意杜绝一切商业用途,如果需要商业用途,请联系作者本人!! 3本文分为几个部分: (1)解决什么问题 (2

SHUOJ A序列 &amp;&amp; POJ 1836 Alignment [动态规划 LIS]

A序列 发布时间: 2017年7月8日 21:16   最后更新: 2017年7月8日 22:29   时间限制: 1000ms   内存限制: 128M 描述 如果一个序列有奇数个正整数组成,不妨令此序列为a1,a2,a3,...,a2?k+1 (0<=k ),并且a1,a2...ak+1 是一个严格递增的序列,ak+1,ak+2,...,a2?k+1 ,是一个严格递减的序列,则称此序列是A序列. 比如1 2 5 4 3就是一个A序列. 现在Jazz有一个长度为n 的数组,他希望让你求出这个数

人脸识别技术大总结1——Face Detection &amp; Alignment

搞了一年人脸识别,寻思着记录点什么,于是想写这么个系列,介绍人脸识别的四大块:Face detection, alignment, verification and identification(recognization),本别代表从一张图中识别出人脸位置,把人脸上的特征点定位,人脸校验和人脸识别.(后两者的区别在于,人脸校验是要给你两张脸问你是不是同一个人,人脸识别是给你一张脸和一个库问你这张脸是库里的谁. 今天先介绍第一部分和第二部分. 主要说三篇顶会文章. ===============

[Sequence Alignment Methods] Dynamic time warping (DTW)

本系列介绍几种序列对齐方法,包括Dynamic time warping (DTW),Smith–Waterman algorithm,Cross-recurrence plot Dynamic time warping (DTW) is a well-known technique to find an optimal alignment between two given (time-dependent) sequences under certain restrictions. ——Mei

存储结构中的对齐(alignment)

最近,在测试基于ceph的小文件合并方案(见上个博文)时,遇到一个怪异的现象:将librados提供的append接口与我们封装的WriteFullObj接口(osd端是append操作和kvdb的put操作)对比,在处理同样大小的文件时(如4KB,8KB等),WriteFullObj比librados的append操作tps低很多,最初怀疑可能是kvdb的put操作的原因,后来将osd端kvdb的put临时去掉,tps仍然上不去:后来使用iostat观察osd上状态,发现WriteFullOb

drawInRect:withFont:lineBreakMode:alignment is deprecated

CGRect myRect = CGRectMake(5,10,200,100) 将 [myText drawInRect:myRect withFont:myFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight]; 改写成: NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle]muta