您的位置:首页 > 其它

poj 1836 Alignment

2016-08-19 21:43 337 查看
Alignment

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16033 Accepted: 5240
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

/*
这题的本质是:给定一个序列,去掉一些项使其成为先递增后递减的子序列,求去掉项的最小数目。(增序列和减序列没有重复元素且必存在增减)

具体步骤:将原序列
[0...N) 从 i (1 <= i <= n - 2) 处一分为二,求 [0...i](递增)与 (i...N)(递减)子序列长度的最大和,此即为符合要求的最长序列。至于最长递增/递减子序列的求法,当然用动规了.

*/
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int n, m;
float a[1005];
int b[1005];
int c[1005];

void Longest()
{
int i, j;
int max2;
memset(b, 0, sizeof(b));
b[0] = 1;
for ( i = 1; i < n; i++ )
{
max2 = 0;
for ( j = 0; j < i; j++ )
{
if ( a[i] > a[j] )
{
max2 = max(max2, b[j]);
}
}
b[i] = max2+1;
}
memset(c, 0, sizeof(c));
c[n-1] = 1;
for ( i = n-2; i >= 0; i-- )
{
max2 = 0;
for ( j = n-1; j > i; j-- )
{
if ( a[i] > a[j] )
{
max2 = max(max2, c[j]);
}
}
c[i] = max2+1;
}
}

int main()
{
int i, j;
while ( ~scanf ( "%d", &n ) )
{
for ( i = 0;i < n; i++ )
{
scanf ( "%f", &a[i] );
}
Longest();
int ans = 0;
for (i = 0; i < n - 1; ++i)
{
for (j = i + 1; j < n; ++j)
{
if ( ans < b[i]+c[j] )
{
ans = b[i] + c[j];
}
}
}
printf ( "%d\n", n-ans );
}
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: