poj 1836 Alignment

来源:互联网 发布:500万双色球过滤软件 编辑:程序博客网 时间:2024/05/22 14:20

首先说一下题意:就是给一队人进行排序,可以从左向右排可以从右向左排,只要是中间的人可以看到两边的人就行,求最少需要有多少人进行位置的调整。

一开始就是以为两边求最长公共子序列、、结果悲催的是有一个小坑、、就是:

8
3 4 5 1 2 5 4 3
答案是:2  

因为中间如果是最高的话也可以看到两边、、所以就是求完两遍最长公共子序列后,求出dp1[i]+dp2[j]的最大值组合,这样就可以使尽量多的人站在队伍之中了啊、、然后n-sum就是最后的结果。

Alignment
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 11489 Accepted: 3658

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

81.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4
#include <stdio.h>#include <string.h>#include <iostream>#include <stdlib.h>#include <cmath>using namespace std;int dp1[1010], dp2[1010];double f[1010];int main(){    int i, j, n;    scanf("%d",&n);    for(i = 0; i < n; i++)    {        scanf("%lf",&f[i]);        dp1[i] = 1;        dp2[i] = 1;    }    for(i = 1; i < n; i++)        for(j = 0; j < i; j++)            if(f[i] > f[j])                dp1[i] = max(dp1[i], dp1[j]+1);    for(i = n-2; i>= 0; i--)        for(j = i+1; j < n; j++)            if(f[i] > f[j])                dp2[i] = max(dp2[i], dp2[j]+1);    int sum = 0;    for(i = 0; i < n-1; i++)        for(j = i+1; j < n; j++)            if(dp1[i]+dp2[j] > sum)                sum = dp1[i]+dp2[j];    printf("%d\n",n-sum);    return 0;}