Alignment ( 最长上升(下降)子序列 )

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11397   Accepted: 3630

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个士兵的身高,要求每个士兵向左或向右能看向无穷远处(新队列呈三角形分布),最少要剔除几个士兵;

思路:对数列分别顺序,逆序求最长上升子序列,然后枚举i和 j,使得以i结尾的上升子序列与以j开头的下降子序列的和最大;

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int main()
 5 {
 6     double a[1010];
 7     int n;
 8     while(~scanf("%d",&n))
 9     {
10         for(int i = 1; i <= n; i++)
11             scanf("%lf",&a[i]);
12         int dp_left[1010],dp_right[1010];
13         
14         //顺序dp求上升子序列
15         for(int i = 1; i <= n; i++)
16         {
17             dp_left[i] = 1;
18             for(int j = 1; j < i; j++)
19             {
20                 if(a[i] > a[j] && dp_left[i] < dp_left[j]+1)
21                     dp_left[i] = dp_left[j]+1;
22 
23             }
24         }
25         
26         //逆序dp求下降子序列;
27         for(int i = n; i >= 1; i--)
28         {
29             dp_right[i] = 1;
30             for(int j = n; j > i; j--)
31             {
32                 if(a[i] > a[j] && dp_right[i] < dp_right[j]+1)
33                     dp_right[i] = dp_right[j]+1;
34             }
35         }
36 
37         //枚举上升子序列的右端点和下降子序列的左端点;
38         int sum = -1;
39         for(int i = 1; i <= n-1; i++)
40         {
41             for(int j = i+1; j <= n; j++)
42             {
43                 if(dp_left[i]+dp_right[j] > sum)
44                     sum = dp_left[i]+dp_right[j];
45             }
46         }
47         printf("%d\n",n-sum);
48     }
49     return 0;
50 }
View Code

  

 

sdut 2403 与上题有些类似;

单峰序列

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

明明最近遇到一个数学问题:给定n个数字(A1,A2,A3......An),每个数字均是小于2^31的正整数,现需要知道这n个数字中的最长单峰子序列长度是多少。所谓单峰序列是指满足如下条件之一的子序列:
(1)Ak1<Ak2<Ak3<......<Akm (k1,k2,k3....km均在1到n之间)
(2)Ak1>Ak2>Ak3>......>Akm (k1,k2,k3....km均在1到n之间)
(3)Ak1<Ak2<Ak3<...<Amid-1<Amid>Amid+1>...>Akm-2>Akm-1>Akm (k1,k2,k3....km均在1到n之间)
现在明明很忙,他想请你帮他解决这个问题,而解决这个问题的好处是他可以让你此时此刻多获得一个彩色气球。你能帮他吗?

输入

输入包含多组数据,每组数据的格式如下:
一个正整数n(1<=n<=1000),表示有多少个数字。
紧跟一行有n个正整数A1,A2....An。(0<=Ai<=2^31)

输出

对于每组输入,输出一个正整数ans,表示该组数据中最长单峰子序列的长度是多少。每个输出占一行。

示例输入

2
1 2
3
1 3 2
4
1 5 4 6

示例输出

2
3
3

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int main()
 5 {
 6     int a[1010];
 7     int n;
 8     while(~scanf("%d",&n))
 9     {
10         for(int i = 1; i <= n; i++)
11             scanf("%d",&a[i]);
12         int dp_left[1010],dp_right[1010];
13         for(int i = 1; i <= n; i++)
14         {
15             dp_left[i] = 1;
16             for(int j = 1; j < i; j++)
17             {
18                 if(a[i] > a[j] && dp_left[i] < dp_left[j]+1)
19                     dp_left[i] = dp_left[j]+1;
20 
21             }
22         }
23 
24         for(int i = n; i >= 1; i--)
25         {
26             dp_right[i] = 1;
27             for(int j = n; j > i; j--)
28             {
29                 if(a[i] > a[j] && dp_right[i] < dp_right[j]+1)
30                     dp_right[i] = dp_right[j]+1;
31             }
32         }
33 
34         int sum = -1;
35         for(int i = 1; i <= n; i++)
36         {
37             int tmp = dp_left[i]+dp_right[i]-1;
38             if(sum < tmp)
39                 sum = tmp;
40         }
41         printf("%d\n",sum);
42     }
43     return 0;
44 }
45  
View Code

 

posted on 2013-08-28 16:22  straw_berry  阅读(258)  评论(0编辑  收藏  举报