zoukankan      html  css  js  c++  java
  • BZOJ 1046 [HAOI2007]上升序列(LIS + 贪心)

    题意:

    m次询问,问下标最小字典序的长度为x的LIS是什么

    n<=10000, m<=1000

    思路:

    先nlogn求出f[i]为以a[i]开头的LIS长度

    然后贪心即可,复杂度nm

    我们正常求LIS处理出的low[i]为长度为i的LIS结尾最小元素,f[i]为以a[i]结尾的LIS长度

    为了迎合题目要求,我们从数组结尾开始求最长下降子序列,得到的f[i]为以结尾开头的,以a[i]结尾的最长下降子序列,等效于以a[i]开头的LIS

    其中为了不手写二分调半天。。使用了lower_bound,此时因为是找最长下降子序列,所以变成负的即可

    代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<stack>
    #include<queue>
    #include<deque>
    #include<set>
    #include<vector>
    #include<map>
    #include<functional>
        
    #define fst first
    #define sc second
    #define pb push_back
    #define mem(a,b) memset(a,b,sizeof(a))
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define lc root<<1
    #define rc root<<1|1
    #define lowbit(x) ((x)&(-x)) 
    
    using namespace std;
    
    typedef double db;
    typedef long double ldb;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> PI;
    typedef pair<ll,ll> PLL;
    
    const db eps = 1e-6;
    const int mod = 1e9+7;
    const int maxn = 2e6+100;
    const int maxm = 2e6+100;
    const int inf = 0x3f3f3f3f;
    const db pi = acos(-1.0);
    
    int n, m;
    int a[maxn], low[maxn];
    int f[maxn];
    int main(){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
        }
        int cnt = 0;
        for(int i = n; i >= 1; i--){
            int t = lower_bound(low+1, low+1+cnt, -a[i]) - low;
            f[i] = t;
            cnt = max(cnt, t);
            low[t] = -a[i];
        }
        scanf("%d", &m);
        for(int i = 1; i <= m; i++){
            int x;
            scanf("%d", &x);
            if(x > cnt)printf("Impossible
    ");
            else{
                int tmp = -1;
                for(int j = 1; j <= n; j++){
                    if(x && f[j] >= x && a[j] > tmp){
                        x--;
                        tmp = a[j];
                        printf("%d ",a[j]);
                    }
                }printf("
    ");
            }
        }
        return 0;
    }
    
    /*
    6
    3 4 1 2 3 6
    3
    6
    4
    5
     */
  • 相关阅读:
    Collectors.reducing总结
    Ubuntu 换源看这一篇就够了
    基于Vue2和Node.js的反欺诈系统设计与实现
    Flink源码解析(四)——从Flink集群部署和任务提交模式看Flink任务的核心组件
    SaaS架构(二) 多租户数据隔离方案
    网络IO模型(BIO,NIO,AIO)
    Gale-Shapley算法
    Java 内存模型
    上位机那些事儿
    三菱PLC之SLMP协议报文说明
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/10137962.html
Copyright © 2011-2022 走看看