Bzoj1046: [HAOI2007]上升序列
阿新 • • 發佈:2018-01-26
for def names putc mark www. gist get 最長
題面
傳送門
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]上升序列