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\n");
        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("\n");
        }
    }
    return 0;
}

/*
6
3 4 1 2 3 6
3
6
4
5
 */

 

posted @ 2018-12-18 16:20  wrjlinkkkkkk  阅读(152)  评论(0编辑  收藏  举报