• BZOJ1046: [HAOI2007]上升序列(LIS)


    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 5740  Solved: 2025
    [Submit][Status][Discuss]

    Description

      对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax
    2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给
    出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先
    x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.

    Input

      第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M
    行每行一个数L,表示要询问长度为L的上升序列。N<=10000,M<=1000

    Output

      对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.

    Sample Input

    6
    3 4 1 2 3 6
    3
    6
    4
    5

    Sample Output

    Impossible
    1 2 3 6
    Impossible

    HINT

    Source

    非常套路的一道题

    它的字典序是按下标排的,因此我们需要预处理出$f[i]$表示从$i$开始最长上升子序列的长度

    这个可以通过倒着求最长下降子序列来实现

    然后询问的时候若当前的$f[i]>=x$那么可以更新答案,直接暴力找就行

    // luogu-judger-enable-o2
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    #define LL long long 
    using namespace std;
    const int MAXN = 10001, INF = 1e9 + 10;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, a[MAXN], low[MAXN], len, bg[MAXN], pre[MAXN];
    int Find(int x) {
        int l = 1, r = len, ans = len;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(x >= low[mid]) r = mid - 1, ans = mid;
            else l = mid + 1;
        }
        return ans;
    } 
    main() { 
        memset(low, 0x3f, sizeof(low));
        N = read();
        for(int i = 1; i <= N; i++) a[i] = read();
        for(int i = N; i >= 1; i--) {
            if(a[i] < low[len]) low[++len] = a[i], bg[i] = len;
            else {
                int pos = Find(a[i]); // mmp这儿不能用stl。。 
                low[pos] = max(low[pos], a[i]);
                bg[i] = pos;
            }
        }
        int M = read();
        while(M--) {
            int x = read();
            if(x > len) {puts("Impossible"); continue;}
            for(int i = 1, j; i <= N; i++) {
                if(bg[i] >= x) {
                    int pre = a[i];
                    printf("%d ", a[i]);
                    if(x == 1) {puts(""); break;}
                    for(int j = i + 1; j <= N; j++) {
                        if(bg[j] >= x - 1 && a[j] > a[i]) 
                            {x--, i = j - 1; break;}
                    }
                }
            }
        }
        return 0;
    }   
  • 相关阅读:
    100行代码实现了多线程,批量写入,文件分块的日志方法
    阿里云客户端开发技巧
    阿里云客户端的实现(支持文件分块,断点续传,进度,速度,倒计时显示)
    类库间无项目引用时,在编译时拷贝DLL
    数据库-锁的实践
    Node.js学习资料
    文档流转,文档操作,文档归档(一)
    滑动验证码研究-后续
    iTextSharp 116秒处理6G的文件
    在职场中混,"讲演稿"的重要性
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9286107.html
Copyright © 2020-2023  润新知