SHUOJ A序列 && 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 的数组,他希望让你求出这个数组所有满足A序列定义的子序列里面最大的那个长度。(子序列可以不连续)

比如1 2 5 4 3 6 7 8 9,最长的A序列子串是1 2 5 4 3。

输入

多组输入,每组两行。
第一行是n ,表示给的数组的长度。
第二行有n 个数(int范围),即给你的数组。
1<=n<=500000 。

输出

每组输入输出一行,即最长的A序列子串的长度。

样例输入1 复制

9
1 2 5 4 3 6 7 8 9

样例输出1

5

这题一看就知道时LIS,ld[i]表示以第i个元素结尾的最长上升子序列的长度,rd[i]表示以第i个元素开始的最长上升子序列的长度,结果是min(ld[i],rd[i]) * 2 + 1 的最大值。但是LIS有两种姿势,一种是O(n^2),一种是O(nlogn)。这题用O(n^2)果断超时。O(nlogn)的方式是维护一个单调栈,当前考虑的值在单调栈中应该在的位置就是此处的最长的长度。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ld[500010],rd[500010];
int s[500010];
int a[500010];
int n;
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        int cnt = 1;
        ld[1] = 1;
        rd[n] = 1;
        s[cnt] = a[1];
        for(int i = 2; i <= n; i++)
        {
            if(a[i] > s[cnt])
            {
                s[++cnt] = a[i];
                ld[i] = cnt;
            }
            else
            {
                int k = lower_bound(s+1, s + cnt + 1, a[i]) - s;
                ld[i] = k;
                s[k] = a[i];
            }
        }
        cnt = 1;
        s[cnt] = a[n];
        for(int i = n - 1; i >= 1; i--)
        {
            if(a[i] > s[cnt])
            {
                s[++cnt] = a[i];
                rd[i] = cnt;
            }
            else
            {
                int k = lower_bound(s+1, s + cnt +1, a[i]) - s;
                rd[i] = k;
                s[k] = a[i];
            }
        }

        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            ans =max(ans, min(2*ld[i]-1 , 2*rd[i]-1));
        }
        cout<<ans<<"\n";
    }
    return 0;
}

然后,有道题跟它类似

Alignment

POJ - 1836

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
当时我用的是O(n^2)的算法,在此处贴出来(丑

)
#include<iostream>
#include<cstdio>
using namespace std;
#include<algorithm>
const int maxn=1005;
const int inf=0x3f3f3f3f;
double h[maxn];
int pre[maxn],next[maxn];

int main()
{
    int n;
    while(EOF!=scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf",h+i);
        }
        pre[0]=next[n-1]=1;
        for(int i=1;i<n;i++)
        {
            pre[i]=1;
            for(int j=0;j<i;j++)
            {
                if(h[i]>h[j])
                    pre[i]=max(pre[i],pre[j]+1);
            }
        }
        for(int i=n-2;i>=0;i--)
        {
            next[i]=1;
            for(int j=n-1;j>i;j--)
            {
                if(h[i]>h[j])
                    next[i]=max(next[i],next[j]+1);
            }
        }
        int ans=inf;
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                ans=min(ans,n-next[j]-pre[i]);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-23 23:49:59

SHUOJ A序列 && POJ 1836 Alignment [动态规划 LIS]的相关文章

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 排队

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 //动态规划 之 双向

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 最长递增子序列(LIS)的变形

大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 5 4 3 2 1 这个序列从左至右看前半部分是递增,从右至左看前半部分也是递增.所以我们先把从左只右和从右至左的LIS分别求出来. 如果结果是这样的: A[i]={1.86 1.86 1.30621 2 1.4 1 1.97 2.2} //原队列 a[i]={1 1 1 2 2 1 3 4} b[

POJ 1836 Alignment 枚举中间点双向求LIS

点击打开链接 Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13590   Accepted: 4375 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

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 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

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 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