Alignment ( 最长上升(下降)子序列 )
2013-08-28 16:22
281 查看
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11397 | Accepted: 3630 |
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开头的下降子序列的和最大;
#include<stdio.h> #include<string.h> int main() { int a[1010]; int n; while(~scanf("%d",&n)) { for(int i = 1; i <= n; i++) scanf("%d",&a[i]); int dp_left[1010],dp_right[1010]; for(int i = 1; i <= n; i++) { dp_left[i] = 1; for(int j = 1; j < i; j++) { if(a[i] > a[j] && dp_left[i] < dp_left[j]+1) dp_left[i] = dp_left[j]+1; } } for(int i = n; i >= 1; i--) { dp_right[i] = 1; for(int j = n; j > i; j--) { if(a[i] > a[j] && dp_right[i] < dp_right[j]+1) dp_right[i] = dp_right[j]+1; } } int sum = -1; for(int i = 1; i <= n; i++) { int tmp = dp_left[i]+dp_right[i]-1; if(sum < tmp) sum = tmp; } printf("%d\n",sum); } return 0; }
View Code
相关文章推荐
- poj 1836 Alignment( 最长上升(下降)子序列 )
- POJ 1836 Alignment 【最长上升(下降)子序列】
- POJ 1836 Alignment 最长上升(下降)子序列(dp)
- 淦!最长上升\下降\不上升\不下降序列
- POJ - 1836 Alignment (最长上升子序列 O(nlogn))
- LIS(Longest Increasing Subsequence)最长上升(不下降)子序列
- 最长上升下降子序列
- BZOJ1046(HAOI2007)[上升序列]--最长下降子序列预处理
- poj 1836 Alignment (最长上升子序列 N*log(n))@
- 最长上升或下降子序列解法 总结
- 最长不上升(下降)子序列——拦截导弹
- hdu 4604 Deque(最长上升与下降子序列-能够重复)
- 蓝桥杯算法训练拦截导弹【最长上升子序列 & 最长非下降子序列nlogn 和 n^2】
- 最长上升序列、最长下降序列及其变形模板
- joj 2529 Chorus 动态规划 最长上升子序列和最长下降子序列
- 最长上升子序列、最长下降子序列的DP算法由O(n^2)到O(nlogn)算法
- 九度OJ 1131:合唱队形 (DP、最长上升下降序列)
- 如何用O(nlogn)的的速度求解最长上升(下降)子序列
- tyvj 1208 最长不下降子序列2 求序列b1,b2,b3,…,bm中所有长度(n)最大上升子序列的个数
- 九度OJ 1131:合唱队形 (DP、最长上升下降序列)