zoukankan      html  css  js  c++  java
  • 第15届浙江省赛 E LIS

    LIS

    Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

    DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence  of length .

    Recall that

    • A subsequence  of length  is a sequence satisfying  and .

    • An increasing subsequence  is a subsequence satisfying .

    DreamGrid defines the helper sequence  where  indicates the maximum length of the increasing subsequence which ends with . In case you don't know how to derive the helper sequence, he provides you with the following pseudo-code which calculates the helper sequence.

    procedure lis_helper(: original sequence)
    {Let  be the length of the original sequence,
     be the -th element in sequence , and 
    be the -th element in sequence }
    for  := 1 to 
         := 1
        for  := 1 to ( - 1)
            if  and 
                 :=  + 1
    return  { is the helper sequence}

     

    DreamGrid has derived the helper sequence using the program, but the original sequence  is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences  and  indicating that  for all .

    Please help DreamGrid restore the original sequence which is compatible with the helper sequence and the two range sequences.

    Input

    There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

    The first line contains an integer  (), indicating the length of the original sequence.

    The second line contains  integers  () seperated by a space, indicating the helper sequence.

    For the following  lines, the -th line contains two integers  and  (), indicating the range sequences.

    It's guaranteed that the original sequence exists, and the sum of  of all test cases will not exceed .

    Output

    For each test case output one line containing  integers separated by a space, indicating the original sequence. If there are multiple valid answers, print any of them.

    Please, DO NOT print extra spaces at the end of each line, or your solution may be considered incorrect!

    Sample Input

    4
    6
    1 2 3 2 4 3
    0 5
    2 4
    3 3
    1 2
    3 5
    1 5
    5
    1 2 1 3 1
    100 200
    200 300
    200 400
    400 500
    100 500
    7
    1 2 3 1 1 4 2
    0 3
    0 3
    0 3
    0 3
    0 3
    0 3
    0 3
    2
    1 1
    1 2
    2 3
    

    Sample Output

    1 2 3 2 5 3
    200 300 200 500 200
    0 1 2 0 0 3 1
    2 2

    题解:  
    若i位置有fi,ai,则之前若有j<i且fi==fj,则ai<=aj  
    若有fi==fj+1,则ai>aj  
    只需从后往前遍历一遍ai<=aj确定一个下限,然后  
    从前到后跑一边ai<aj更新下限即可  
    代码:  
      
    #include<bits/stdc++.h>  
    using namespace std;  
    #define ll long long  
    const int maxn=1e5+7;  
    int a[maxn],d[maxn],L[maxn],pre[maxn];  
    int main()  
    {  
        int T;scanf("%d",&T);  
        while(T--)  
        {  
            memset(pre,-1,sizeof(pre));  
            int n;scanf("%d",&n);  
            for(int i=1;i<=n;i++)scanf("%d",&a[i]);  
            for(int i=1;i<=n;i++)  
            {  
                int x;scanf("%d%d",&L[i],&x);  
            }  
            for(int i=n;i>=1;i--)  
            {  
                d[i]=L[i];  
                if(pre[a[i]]!=-1)d[i]=max(d[pre[a[i]]],L[i]);  
                pre[a[i]]=i;  
            }  
            memset(pre,-1,sizeof(pre));  
            for(int i=1;i<=n;i++)  
            {  
                int x=pre[a[i]-1];  
                if(x!=-1)d[i]=max(d[i],d[x]+1);  
                pre[a[i]]=i;  
            }  
            for(int i=1;i<=n;i++)  
            {  
                printf("%d",d[i]);  
                if(i!=n)printf(" ");  
                else printf("
    ");  
            }  
        }  
        return 0;  
    }  
  • 相关阅读:
    acdream 瑶瑶带你玩激光坦克 (模拟)
    acdream 小晴天老师系列——苹果大丰收(DP)
    acdream 小晴天老师系列——晴天的后花园 (暴力+剪枝)
    acdream 小晴天老师系列——竖式乘法(简单穷举)
    acdream LCM Challenge (最小公倍数)
    LeetCode Product of Array Except Self (除自身外序列之积)
    LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
    字节流与字符流的区别
    oop第二章1知识点汇总
    抽象类和抽象方法的一些概念(转自百度)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9007651.html
Copyright © 2011-2022 走看看