您的位置:首页 > 其它

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

2013-08-28 16:22 281 查看
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 11397Accepted: 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开头的下降子序列的和最大;


#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: