poj 1836 Alignment(线性dp)

题目链接:http://poj.org/problem?id=1836

思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0] < B[1] <….<B[i] 且 B[i+1] > … > B[m];

该问题实质为求A[0, …, k]的最长递增子序列和A[j, …, n]中的最长递减子序列(0 <= k <= n, 0 <= j <= n, k < j);所以求出A[0, .., k]的最长递增子序列

与A[j, …, n]中的最长递减子序列,在枚举k与j的值,求出最大的和,在用人数减去最大和即可;

代码如下:

#include <iostream>
using namespace std;

const int MAX_N = 1000 + 10;
double num[MAX_N];
int dp_l[MAX_N], dp_r[MAX_N];

inline int Max(int a, int b) { return a - b > 0 ? a : b; }
int main()
{
    int n;

    while (scanf("%d", &n) != EOF)
    {
        for (int i = 0; i < n; ++ i)
            scanf("%lf", &num[i]);

        for (int i = 0; i < n; ++ i)
        {
            dp_l[i] = 1;
            for (int j = 0; j < i; ++ j)
            {
                if (num[j] < num[i])
                    dp_l[i] = Max(dp_l[i], dp_l[j] + 1);
            }
        }
        for (int i = n - 1; i >= 0; -- i)
        {
            dp_r[i] = 1;
            for (int j = n - 1; j > i; -- j)
            {
                if (num[j] < num[i])
                    dp_r[i] = Max(dp_r[i], dp_r[j] + 1);
            }
        }

        int ans = 0;
        for (int i = 0; i < n; ++ i)
        {
            int temp = dp_l[i];
            for (int j = i + 1; j < n; ++ j)
            {
                if (temp + dp_r[j] > ans)
                    ans = temp + dp_r[j];
            }
        }
        ans = Max(ans, dp_l[n - 1]);
        ans = Max(ans, dp_r[0]);

        printf("%d\n", n - ans);
    }
    return 0;
}
时间: 2024-10-31 00:45:05

poj 1836 Alignment(线性dp)的相关文章

poj 1836 Alignment(dp,LIS)

链接:poj 1836 题意:士兵站成一行,求最少要多少的士兵出列, 使得每个士兵都能至少看到一个最边上的士兵 中间某个人能看到最边上的士兵的条件是: 该士兵的身高一定强大于他某一边(左边或右边)所有人的身高, 身高序列可以是: 1  2  3   4   5   4   3   2   1  或者 1   2   3   4   5   5   4   3   2   1 分析:要求最少出列数,就是留队士兵人数最大, 即左边的递增序列人数和右边的递减序列人数之和最大 因而可转化为求"最长升序子

POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))

Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14486   Accepted: 4695 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 cap

poj 1836 Alignment 排队

poj   1836   Alignment http://poj.org/problem?id=1836 题意:有士兵n个,根据编号排为一列,但是身高不一,现在要求去掉几个人,使得剩下的每一个人可以向左或向右看到队头.问:至少去掉的士兵数. dp动态规划 之 双向LIS问题 1 /* 2 Problem: 1836 User: bibier 3 Memory: 712K Time: 63MS 4 Language: G++ Result: Accepted 5 */ 6 //动态规划 之 双向

SHUOJ A序列 &amp;&amp; POJ 1836 Alignment [动态规划 LIS]

A序列 发布时间: 2017年7月8日 21:16   最后更新: 2017年7月8日 22:29   时间限制: 1000ms   内存限制: 128M 描述 如果一个序列有奇数个正整数组成,不妨令此序列为a1,a2,a3,...,a2?k+1 (0<=k ),并且a1,a2...ak+1 是一个严格递增的序列,ak+1,ak+2,...,a2?k+1 ,是一个严格递减的序列,则称此序列是A序列. 比如1 2 5 4 3就是一个A序列. 现在Jazz有一个长度为n 的数组,他希望让你求出这个数

POJ 2479-Maximum sum(线性dp)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

poj 1836 Alignment (DP LIS)

Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13839   Accepted: 4478 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 cap

[poj 1836] Alignment dp

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

POJ 1836 Alignment

http://poj.org/problem?id=1836 题意:给出一排士兵的身高,求出至少需要移除多少个士兵可以使得剩下的士兵往左看或者是往右看可以看到无穷远处. 思路:士兵的分布最终要呈三角形分布,我们从左边和右边分别求一个最长递增子序列,然后最后只需要一一枚举就可以了. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int maxn = 1000 + 5; 6 7 i

POJ 2250 Compromise (线性dp LCS +递归路径)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6735   Accepted: 3009   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 fu