您的位置:首页 > 其它

POJ 1836 Alignment

2016-08-16 21:36 399 查看
Alignment

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 15867 Accepted: 5177
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<cstdio>
#include<cstring>

using namespace std;

double a[11234];
int dp1[12345],dp2[12345];

int main()
{
int n,s=0;  cin>>n;
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
dp1[1]=dp2
=1;
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
for(int i=2;i<=n;i++)
{
int s=0;
for(int j=1;j<i;j++) if(a[i]>a[j]&&dp1[j]>s) s=dp1[j];
dp1[i]=s+1;
}
for(int i=n-1;i>=1;i--)
{
int s=0;
for(int j=n;j>i;j--) if(a[i]>a[j]&&dp2[j]>s) s=dp2[j];
dp2[i]=s+1;
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++) s=max(s,dp1[i]+dp2[j]);
cout<<n-s<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 算法 动态规划