poj1836--Alignment(dp,最长上升子序列变形)

Alignment

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13319   Accepted: 4282

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

Romania OI 2002

题目要求:给出n个人排成一排,踢出一些人,让每个人都能看到最左端,或最后端,最小的踢出人数是?

计算出正序和倒序的最长上升子序列,然后统计:有两种可能,一种是其中一个人是中间,那个人的身高最高,还有事两个人的身高相同,这两个人位置在中间,统计出最长的可能出现的队伍长度,计算出最小的踢出人数

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp1[1200] , dp2[1200] ;
double h[1200] ;
int main()
{
    int i , j , n , min1 ;
    while(scanf("%d", &n)!=EOF)
    {
        min1 = 1200 ;
        h[0] = 0 ; h[n+1] = 0 ;
        for(i = 1 ; i <= n ; i++)
            scanf("%lf", &h[i]);
        memset(dp1,0,sizeof(dp1));
        for(i = 1 ; i <= n ; i++)
        {
            for(j = 0 ; j < i ; j++)
                if( h[j] < h[i] && dp1[j]+1 > dp1[i] )
                    dp1[i] = dp1[j]+1 ;
        }
        memset(dp2,0,sizeof(dp2));
        for(i = n ; i >= 1 ; i--)
        {
            for(j = n+1 ; j > i ; j--)
                if( h[j] < h[i] && dp2[j]+1 > dp2[i] )
                    dp2[i] = dp2[j]+1 ;
        }
        for(i = 1 ; i <= n ; i++)
        {
            for(j = i ; j <= n ; j++)
            {
                if(i == j)
                    min1 = min(min1,n-(dp1[i]+dp2[j]-1) );
                else
                    min1 = min(min1, n-( dp1[i]+dp2[j] ) );
            }
        }
        printf("%d\n", min1);
    }
}
时间: 2024-10-07 03:18:24

poj1836--Alignment(dp,最长上升子序列变形)的相关文章

poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53414   Accepted: 18449 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a

poj1836——dp,最长上升子序列(lis)

poj1836——dp,最长上升子序列(lis) Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13767   Accepted: 4450 Description In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight

HDU 1160 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了. 题目思路:这是个最长上升子序列的问题,我们按W的升序进行排序,若W相等则按V的降序排序.用Pre[]记录当前点的前驱节点,Last记录序列最后一个点,maxn记录最长长度,完成动规后可根据Last和Pre[]输出路径. #include<cstdio> #include<stdio.h&

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

codeforces340D - Bubble Sort Graph dp + 最长上升子序列

题意:给你长为n的序列 ,每个节点都和在它前面且值比他大的点产生一条边,问你一个最大 两两点没有边的集合的 集合元素有多少 解题思路:想了半天才发现是最长上升子序列.. 解题代码: 1 // File Name: 340d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 12时31分24秒 4 5 #include<vector> 6 #include<list> 7 #include<map>

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in

vijos P1028 最长上升子序列变形题

#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<string> #include<vector> #include<algorithm> using namespace std; bool judge(const string &aa,const string &bb) { for(int i=0;i&