POJ 1836 Alignment 士兵队列

 

题目内容
Alignment
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7320 Accepted: 2319
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

Romania OI 2002

简单题意:给出一列士兵的身高,求怎样安排才能在最少的n名士兵出列后,使剩下的每名士兵向左或向右的视线不会被其他士兵阻挡。(如果a的身高高于b的身高,则a的视线不会被b阻挡,反之b会挡住a)

类似于合唱队形,也是求LIS最长上升子序列和LDS最长下降子序列。

我的算法思想:求出对于位置x,从开头到x的LIS和x+1到队尾的LDS,找出最大的两个,则答案就是总士兵人数-LIXmax-LDSmax。

成绩:Accepted 416K 63MS G++ 783B

编译环境:Linux ubuntu 2.6.35-30-generic,g++ 4.4.5,KDevelop IDE

另外,推荐一下这篇文章

http://www.felix021.com/blog/read.php?1587

思路很好

View Code
 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 long N;
5 double soid[1001];
6 long li[1001],ld[1001];
7 long ans;
8 void init()
9 {
10 scanf("%ld",&N);
11 for(long i=0;i!=N;i++)
12 scanf("%lf",&soid[i]);
13 ans=0;
14 for(long i=0;i!=N;i++)
15 li[i]=ld[i]=1;
16 }
17 void produce()
18 {
19 for(long i=1;i<=N;i++)
20 for(long j=0;j!=i;j++)
21 if(soid[i]>soid[j])
22 li[i]=li[i]>li[j]+1?li[i]:li[j]+1;
23 for(long i=N;i!=0;i--)
24 for(long j=N+1;j!=i;j--)
25 if(soid[i]>soid[j])
26 ld[i]=ld[i]>ld[j]+1?ld[i]:ld[j]+1;
27 }
28 void solve()
29 {
30 long i,j,lis,lds;
31 for(lis=0,i=0;i!=N;i++)
32 {
33 lis=lis>li[i]?lis:li[i];
34 for(lds=0,j=i+1;j!=N;j++)
35 lds=lds>ld[j]?lds:ld[j];
36 ans=ans>lis+lds?ans:lis+lds;
37 }
38 }
39 int main()
40 {
41 init();
42 produce();
43 solve();
44 printf("%ld\n",N-ans);
45 return 0;
46 }



posted on 2011-11-03 21:04  SilVeRyELF  阅读(493)  评论(0编辑  收藏  举报

导航