[POJ 1836]Alignment[DP][LIS]

题目链接: [POJ 1836]Alignment[DP][LIS]

题意分析:

为了使得士兵的队列满足:任意一个士兵都能看到他前方的无穷远处或者后方的无穷远处(大概是个三角形的样子)。最少需要移出去多少名士兵?

解题思路:

求出原序列的最长上升和最长下降子序列,然后枚举左右士兵即可。

个人感受:

怎么枚举士兵这点没处理好,之前思维卡在三角形只有一个最高点,其实本题并非完全让队列变成三角形,可以有两个等高,因为题目说的是前或后。

具体代码如下:

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define lowbit(x) (x & (-x))
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1  1
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;

const int MAXN = 1e3 + 11;

int dp[MAXN];
int dp2[MAXN];
double h[MAXN];

int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);
    #endif
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%lf", &h[i]);
    }
    for (int i = 0; i < n; ++i) {
        dp[i] = 1;
        for (int j = 0; j < i; ++j) {
            if (h[i] > h[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }

    for (int i = n - 1; i >= 0; --i) {
        dp2[i] = 1;
        for (int j = n - 1; j > i; --j) {
            if (h[i] > h[j]) {
                dp2[i] = max(dp2[i], dp2[j] + 1);
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            ans = max(ans, dp[i] + dp2[j]);
        }
    }
    printf("%d\n", n - ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值