POJ - 1836 Alignment

题意:令原队列的最少士兵出列后,使得新队列任意一个士兵都能看到左边或者右边的无穷远处。就是使新队列呈三角形分布就对了。就是分别从两边求最长上升子序列就对了,然后减去max行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1005;

int dp1[MAXN],dp2[MAXN];
int n;
double num[MAXN];

int main(){
    while (scanf("%d",&n) != EOF){
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        for (int i = 1; i <= n; i++)
            scanf("%lf",&num[i]),dp1[i]=1,dp2[i]=1;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j < i; j++)
                if (num[i] > num[j])
                    dp1[i] = max(dp1[i],dp1[j]+1);
        for (int i = n-1; i >= 1; i--)
            for (int j = n; j > i; j--)
                if (num[i] > num[j])
                    dp2[i] = max(dp2[i],dp2[j]+1);
        int ans = 0;
        for (int i = 1; i < n; i++)
            for (int j = i+1; j <= n; j++)
                ans = max(ans,dp1[i]+dp2[j]);
        printf("%d\n",n-ans);
    }
    return 0;
}



你可能感兴趣的:(POJ - 1836 Alignment)