现在的位置: 首页 > 综合 > 正文

poj 1836 Alignment 最长子序列的动态规划

2013年11月11日 ⁄ 综合 ⁄ 共 2593字 ⁄ 字号 评论关闭

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11779   Accepted: 3756

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

原文是说有n个编号的士兵站成一列,士兵可以向左看,向右看,求最少删除几个士兵可以使这一列的所有士兵都能向左或者向右看到尽头(没有一个其他的士兵比他高),我们可以转化为最多剩多少士兵,所以就成了求最长升序子序列的变形,做这道题首先做一下最简单的求最长升序子序列的题比如poj2533点击打开链接简单的最长子  序列

这道题的解题方法

#include<stdio.h>
#include<iostream>
using namespace std;
int dp[1005];
int num[1005];
int max(int a,int b)
{
	return a>b? a:b;
}
int main()
{
	int i,j,n,max,Max;
	while(scanf("%d",&n)!=EOF)
	{
		dp[1]=1;
		Max=0;
		for(i=1;i<=n;i++)
			scanf("%d",&num[i]);
		max=0;
		for(i=1;i<=n;i++)
		{
			dp[i] = 1;
			for(j=i-1;j>=1;j--)
			{
				if(num[i]>num[j] && dp[i]<dp[j]+1)
					dp[i]=dp[j]+1;
				
			}
			max = max<dp[i]?dp[i]:max;
		}
		printf("%d\n",max);
	}
	return 0;
}

好了言归正传,poj1836点击打开题目链接这道题看上去比前一道难点,不过我用了两次最长升序子序列dp1[1-n],dp2[n-1],分别正序反序求出他们的最长升序,然后再在dp1+dp2中寻找最大的,寻找的时候一定要dp2>dp1,因为只有这样你才能符合每个士兵都可以看到尽头

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
int  dp1[1009],dp2[1009];
float num[10];
int main()
{
	int i,j,n,max;
	while(scanf("%d",&n)!=EOF)
	{
		memset(dp1,0,sizeof(dp1));
		memset(dp2,0,sizeof(dp2));
		memset(num,0,sizeof(num));
		max=0;
	
		for(i=1;i<=n;i++)
			scanf("%f",&num[i]);
		for(i=1;i<=n;i++)
		{
			dp1[i]=1;
			for(j=i-1;j>=1;j--)
			{
				if(num[i]>num[j]&& dp1[i]<dp1[j]+1)//dp[i]>dp[j+1]找出0-i中,最大的dp[i]
					dp1[i]=dp1[j]+1;
			}
			
		}
		for(i=n;i>=1;i--)
		{
			dp2[i]=1;
			for(j=i+1;j<=n;j++)
			{
				if(num[i]>num[j]&& dp2[i]<dp2[j]+1)
					dp2[i]=dp2[j]+1;
			}
		}
		for(i=1;i<=n;i++)
			for(j=i+1;j<=n;j++)
		{
			if(dp1[i]+dp2[j]>=max)
				max=dp1[i]+dp2[j];
			
		}
		printf("%d\n",n-max);
		
	}
	return 0;
	
}

抱歉!评论已关闭.