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

BZOJ1046 [HAOI2007]上升序列

时间:2014-10-25 21:27:47      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

啊,noip难度题。。。蒟蒻调了半天。。。

嘛、先是求LIS的长度,O(n * logn)算法大家都会

然后就是贪心,假设我们找到了答案的第x项,向后找第x + 1项:

我们发现,只需找当前最前面的a[i]满足f[i] >= l - x的即可。

bubuko.com,布布扣
 1 /**************************************************************
 2     Problem: 1046
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:1772 ms
 7     Memory:960 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12  
13 using namespace std;
14 const int N = 10005;
15 const int inf = (int) 1e9;
16 int f[N], a[N], b[N];
17 int len, n;
18 int ans[N];
19  
20 inline int read(){
21     int x = 0, sgn = 1;
22     char ch = getchar();
23     while (ch < 0 || ch > 9){
24         if (ch == -) sgn = -1;
25         ch = getchar();
26     }
27     while (ch >= 0 && ch <= 9){
28         x = x * 10 + ch - 0;
29         ch = getchar();
30     }
31     return sgn * x;
32 }
33  
34 void pre_work(){
35     len = 1;
36     int l, r, mid;
37     for (int i = 2; i <= n; ++i)
38         b[i] = -inf;
39     b[0]= inf, b[1] = a[1], f[1] = 1;
40     for (int i = 2; i <= n; ++i){
41         l = 0, r = len + 1;
42         while (l + 1 < r){
43             mid = (l + r) >> 1;
44             if (b[mid] > a[i]) l = mid;
45             else r = mid;
46         }
47         len = max(len, ++l);
48         b[l] = max(b[l], a[i]);
49         f[i] = l;
50     }
51     reverse(a + 1, a + n + 1);
52     reverse(f + 1, f + n + 1);
53 }
54  
55 void work(int l){
56     if (len < l){
57         printf("Impossible\n");
58         return;
59     }
60     int cnt = 0, last = -inf;
61     for (int i = 1; i <= n && l != 0; ++i)
62         if (f[i] >= l && a[i] > last)
63             ans[++cnt] = i, --l, last = a[i];
64     for (int i = 1; i < cnt; ++i)
65         printf("%d ", a[ans[i]]);
66     printf("%d\n", a[ans[cnt]]);
67 }
68  
69 int main(){
70     n = read();
71     for (int i = n; i; --i)
72         a[i] = read();
73     pre_work();
74     int m = read(), X;
75     while (m--){
76         X = read();
77         work(X);
78     }
79     return 0;
80 }
View Code

 

BZOJ1046 [HAOI2007]上升序列

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/rausen/p/4050818.html

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