• [HAOI2007] 上升序列


    给定序列 (S),有 (m) 次询问,每次要求输出下标字典序最小的长度为 (l_i) 的上升序列

    Solution

    考虑设 (f[i]) 表示从 (i) 发出的最长上升子序列的长度,倒序转移$

    转移时,在使得 (f[i]) 最大的情况下,使得 (i) 尽量小,即可使得字典序最小

    每次询问时,检查是否有 (f[i]=l),如果没有则直接判定无解

    否则,从头开始,利用已经预处理出的 (f[i]) 数组,贪心地找出一个下标字典序最小的上升序列即可

    (一开始想着记录转移数组来输出答案,后来发现我憨了)

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1000005;
    int n,m,a[N],f[N],l;
    
    signed main() {
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=n;i>=1;--i) {
            f[i]=1;
            for(int j=i+1;j<=n;j++) {
                if(a[i]<a[j]) {
                    if(f[i]<f[j]+1) f[i]=f[j]+1;
                }
            }
        }
        cin>>m;
        while(m--) {
            cin>>l;
            int t=0;
            for(int i=1;i<=n;i++) {
                if(f[i]==l) t=i;
            }
            if(t==0) cout<<"Impossible"<<endl;
            else {
                int lim=0;
                for(int i=1;i<=n;i++) if(l && f[i]>=l && a[i]>lim) {
                    cout<<a[i]<<" ";
                    lim=a[i];
                    --l;
                }
                cout<<endl;
            }
        }
    }
    
    
  • 相关阅读:
    mysql数据增删改查
    Python中的逻辑运算
    Python的格式化输出
    Python变量的命名规则
    解释型语言和编译型语言的比较?
    SQLAlchemy
    ansible
    算法
    数据分析
    scrapy之日志等级
  • 原文地址:https://www.cnblogs.com/mollnn/p/12381477.html
Copyright © 2020-2023  润新知