bzoj 1047
2015-08-16 23:56
176 查看
求字典序最小,可以想到反向做最长下降子序列,求出 fif_i ,以 aia_i 为起始的最长上升子序列长度。
如果询问的长度 LL 大于 最长上升子序列长度,直接输出 ImpossibleImpossible。
贪心,因为要求字典序最小,对于满足条件 fi≥Lf_i \geq L 的最小的 ii , aia_i 一定是上升序列的首位,
然后在序列的区间 [i+1,n][i+1,n] 内求出长度为 L−1L-1,且首位 aj>aia_j > a_i 的上升序列,递归处理即可。
正确性显然,实现可以非递归。
如果询问的长度 LL 大于 最长上升子序列长度,直接输出 ImpossibleImpossible。
贪心,因为要求字典序最小,对于满足条件 fi≥Lf_i \geq L 的最小的 ii , aia_i 一定是上升序列的首位,
然后在序列的区间 [i+1,n][i+1,n] 内求出长度为 L−1L-1,且首位 aj>aia_j > a_i 的上升序列,递归处理即可。
正确性显然,实现可以非递归。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> #include <utility> #include <stack> #include <queue> #include <iostream> #include <algorithm> template<class Num>void read(Num &x) { char c; int flag = 1; while((c = getchar()) < '0' || c > '9') if(c == '-') flag *= -1; x = c - '0'; while((c = getchar()) >= '0' && c <= '9') x = (x<<3) + (x<<1) + (c-'0'); x *= flag; return; } template<class Num>void write(Num x) { if(x < 0) putchar('-'), x = -x; static char s[20];int sl = 0; while(x) s[sl++] = x%10 + '0',x /= 10; if(!sl) {putchar('0');return;} while(sl) putchar(s[--sl]); } #define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++) const int maxn = 1e4 + 5, INF = 0x3f3f3f3f; int n, m, a[maxn], f[maxn], LIS ,L; void init() { read(n); REP(i, 1, n) read(a[i]); } void prework() { static int max[maxn]; REP(i, 1, n) max[i] = -INF; for(int i = n; i > 0; i--) { int pos = std::lower_bound(max + 1, max + n + 1, a[i], std::greater<int>()) - max; max[f[i] = pos] = a[i], LIS = std::max(pos, LIS); } } void solve() { read(m); REP(i, 1, m) { read(L); if(L > LIS) { puts("Impossible"); continue; } int last = -INF; for(int i = 1; i <= n && L; i++) if(L <= f[i] && a[i] > last) { write(last = a[i]); if(--L) putchar(' '); } puts(""); } } int main() { #ifndef ONLINE_JUDGE freopen("1046.in","r",stdin); freopen("1046.out","w",stdout); #endif init(), prework(), solve(); #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); #endif return 0; }
相关文章推荐
- Python的内置方法,abs,all,any,basestring,bin,bool,bytearray,callable,chr,cmp,complex,divmod
- 【LeetCode】258 - Add Digits
- Frame可视化调用的顺序问题
- JStorm中消息确保处理机制
- [Elasticsearch] 数据建模 - 处理关联关系(1)
- zoj 3827 Information Entropy 【水题】
- libevent源代码分析-介绍、安装、使用
- FCM算法
- Android Studio导入第三方类库的方法
- 项目计划的随笔
- c中字符串分割函数strtok
- 2.Add Two Numbers
- Spring3.0+Struts2.2+Hibernate3.6整合与常见问题
- 嵌入式数据库调研
- Android 开源项目分类汇总
- 多态的特性
- Android view利用canvas绘制动画(一)
- 翻译:AKKA笔记 - Actor消息 -1(一)
- Sea.js
- introduce-kafka