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

Posted 可达龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)的算法,在此处贴出来(丑

Embarrassed smile

)
#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;
}

 

以上是关于SHUOJ A序列 && POJ 1836 Alignment [动态规划 LIS]的主要内容,如果未能解决你的问题,请参考以下文章

线段树区间更新&&求和poj3486

POJ 2567 Code the Tree &amp; POJ 2568 Decode the Tree Prufer序列

POJ1850&&1019&&1942

POJ2533&&1836&&3176

POJ 2182 Lost Cows (树状数组 && 二分查找)

POJ 2299 Ultra-QuickSort (树状数组 && 离散化)