POJ 1836:Alignment

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14492   Accepted: 4698

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

题意是给出了一个序列,希望这个序列满足这个序列中的每一个数,要么是从左到右的最大值,要么是从右到左的最大值。现在不满足,需要从当前序列中抽走几个数重新排能满足上述的条件。

从左到右求一次递增,从右到左求一次递增。求在每一个数之内其从左到右+从右到左 递增序列的最大值。用总和相减即可。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

int num;
int l_dp[2000];
int r_dp[2000];
double value[2000];

int main()
{
	int i,j,max_v;
	scanf("%d",&num);

	for(i=1;i<=num;i++)
	{
		cin>>value[i];
		l_dp[i]=1;
		r_dp[i]=1;
	}
	max_v=0;
	for(i=1;i<=num;i++)
	{
		max_v=0;
		for(j=1;j<i;j++)
		{
			if(value[i]>value[j])
			{
				max_v=max(l_dp[j],max_v);
			}
		}
		l_dp[j]=max_v+1;
	}

	for(i=num;i>=1;i--)
	{
		max_v=0;
		for (j = num; j > i; j--)
		{
			if(value[i]>value[j])
			{
				max_v=max(r_dp[j],max_v);
			}
		}
		r_dp[j]=max_v+1;
	}
	for(i=1;i<=num;i++)
	{
		l_dp[i]=max(l_dp[i],l_dp[i-1]);
	}
	for(i=num;i>=1;i--)
	{
		r_dp[i]=max(r_dp[i],r_dp[i+1]);
	}
	max_v=0;
	for(i=1;i<=num;i++)
	{
		max_v = max(l_dp[i]+r_dp[i+1],max_v);
	}

	cout<<num-max_v<<endl;
	return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785769.html