码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj1046: [HAOI2007]上升序列

时间:2018-01-25 23:16:03      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:max   get   opera   c++   class   possible   []   problem   gpo   

题面

传送门

Sol

先求出最长上升序列,倒着求,然后贪心的往后选,选满足的
求最长上升序列我用的是树状数组

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e4 + 5);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, a[_], m, o[_], len, maxlen;
struct Data{
    int v, p;
    IL bool operator <(RG Data B) const{
        return v != B.v ? v < B.v : p > B.p;
    }
} bit[_], f[_];

IL void Add(RG int x, RG Data mx){
    for(; x <= len; x += x & -x) bit[x] = max(bit[x], mx);
}

IL Data Query(RG int x){
    RG Data ret = (Data){-1, 0};
    for(; x; x -= x & -x) ret = max(bit[x], ret);
    return ret;
}

int main(RG int argc, RG char* argv[]){
    n = Read();
    for(RG int i = 1; i <= n; ++i) o[++len] = a[i] = -Read();
    sort(o + 1, o + len + 1);
    len = unique(o + 1, o + len + 1) - o - 1;
    for(RG int i = 1; i <= n; ++i) a[i] = lower_bound(o + 1, o + len + 1, a[i]) - o;
    f[n] = (Data){1, 0}; Add(a[n], (Data){1, n});
    for(RG int i = n - 1; i; --i){
        f[i] = (Data){1, 0};
        RG Data mx = Query(a[i] - 1);
        f[i] = max(f[i], (Data){mx.v + 1, mx.p});
        Add(a[i], (Data){f[i].v, i});
        maxlen = max(maxlen, f[i].v);
    }
    m = Read();
    for(RG int i = 1; i <= m; ++i){
        RG int l = Read(), mx = -1e9;
        if(l > maxlen) puts("Impossible");
        else{
            for(RG int i = 1; i <= n && l; ++i)
                if(f[i].v >= l && -o[a[i]] > mx){
                    --l;
                    printf("%d", mx = -o[a[i]]);
                    if(l != 0) putchar(' ');
                }
            puts("");
        }
    }
    return 0;
}

Bzoj1046: [HAOI2007]上升序列

标签:max   get   opera   c++   class   possible   []   problem   gpo   

原文地址:https://www.cnblogs.com/cjoieryl/p/8353333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!