poj 1836!!! 5th_problem_B 求最少出列的人数

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

分析:令到原队列的最少士兵出列后,使得新队列任意一个士兵都能看到左边或者右边的无穷远处,即使新队列呈三角形分布。具体步骤为:1.求最长不降子序列  2.求最长不升子序列  3.得到最大的子序列和k,所求的最少出列人数=n-k需要特别注意的是:最中间两个士兵身高可以相同。例如:5 5 5 这种情况,输出结果应为 1。

代码实现:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 const int MAX=1005;
 8 double a[MAX];
 9 int up[MAX],down[MAX];
10
11 int main()
12 {
13     int n,i,j,k;
14     scanf("%d",&n);
15     for(i=1;i<=n;i++)
16     scanf("%lf",&a[i]);
17
18     memset(up,0,sizeof(up));
19     memset(down,0,sizeof(down));
20
21     up[1]=1;
22     for(i=2;i<=n;i++)
23     {
24         up[i]=1;
25         for(j=i-1;j>=1;j--)
26         {
27             if(a[i]>a[j] &&up[i]<up[j]+1)
28                 up[i]=up[j]+1;
29         }
30     }
31
32     down[n]=1;   //注意将down[n]初始化为1
33     for(i=n-1;i>=1;i--)
34     {
35             down[i]=1;
36         for(j=i+1;j<=n;j++)
37         {
38             if(a[i]>a[j] && down[i]<down[j]+1)
39                 down[i]=down[j]+1;
40         }
41     }
42
43     k=up[n];  //k=up[n]而不是up[1];
44     for(i=1;i<n;i++)
45     {
46         for(j=i+1;j<=n;j++)
47         {
48             if(k<up[i]+down[j])
49                 k=up[i]+down[j];
50         }
51     }
52     //cout<<k<<endl;
53     cout<<n-k<<endl;
54     return 0;
55
56 }


				
时间: 2024-11-03 15:18:16

poj 1836!!! 5th_problem_B 求最少出列的人数的相关文章

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r

poj 1836 Alignment(dp,LIS)

链接:poj 1836 题意:士兵站成一行,求最少要多少的士兵出列, 使得每个士兵都能至少看到一个最边上的士兵 中间某个人能看到最边上的士兵的条件是: 该士兵的身高一定强大于他某一边(左边或右边)所有人的身高, 身高序列可以是: 1  2  3   4   5   4   3   2   1  或者 1   2   3   4   5   5   4   3   2   1 分析:要求最少出列数,就是留队士兵人数最大, 即左边的递增序列人数和右边的递减序列人数之和最大 因而可转化为求"最长升序子

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 的数组,他希望让你求出这个数

HDU 1326 Box of Bricks(水~平均高度求最少移动砖)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到平均墙的高度(即最后墙的高度),遍历所有墙,如果小于平均墙,则用平均墙减去高度即是要移动的高度,统计所有需要“补”的高度即可.注意输出. AC Code: 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int

POJ 3461 Oulipo (求模式串在文本串中出现的次数)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36128   Accepted: 14584 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

POJ 2115 for求循环次数-数论-(同余方程+扩展欧几里得算法)

题意:给定for循环的初始值,结束值和增量,还有一个模,求最少的循环次数. 分析: 读完题后应该就知道是一个同余的概念,所以就是解一个一元一次同余方程,像上题一样用扩展欧几里得算法.这题的trick点是k最大为32,那么2^32超出了int,要用long long,所以在1<<k时要这样做:1LL<<k,不然就WA了. 代码: #include<iostream> #include<cstdio> #include<algorithm> #inc

poj 1836

poj 1836#include <iostream>#include <string.h>using namespace std ;const int MAX = 1005 ;double m[MAX] ;int dp1[MAX] , dp2[MAX] ;int main(){ int n ; while ( cin >> n ) { int i , j ; for ( i = 1 ; i <= n ; i++ ) { cin >> m[i] ; d

poj 1836 Alignment 排队

poj   1836   Alignment http://poj.org/problem?id=1836 题意:有士兵n个,根据编号排为一列,但是身高不一,现在要求去掉几个人,使得剩下的每一个人可以向左或向右看到队头.问:至少去掉的士兵数. dp动态规划 之 双向LIS问题 1 /* 2 Problem: 1836 User: bibier 3 Memory: 712K Time: 63MS 4 Language: G++ Result: Accepted 5 */ 6 //动态规划 之 双向

POJ 3978(求素数)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁