【BZOJ】1046: [HAOI2007]上升序列(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1046
一直看错题。。。。。。。。。。。。。。。。。。。。。。。
这是要求位置的字典序啊QQQAAAQQQ
。。
那么就lis后直接从前往后扫就行了。。
注意输出方案不要写错。。(wa了好多发。。。)
拓展:同时如果求答案的字典序最小,那么我们可以先对所有元素排序,然后一个个去试,即维护当前的lis长度,看当前的这个点是否能被接上(因为排序后是单调的,lis也是单调的)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=10005, oo=~0u>>1;
int a[N], f[N], g[N], n, mx;
bool cmp(const int &a, const int &b) { return a>b; }
void lis() {
CC(g, 128);
for3(i, n, 1) {
int t=lower_bound(g+1, g+1+(n-i+1), a[i], cmp)-g; dbg(t);
f[i]=t;
g[t]=a[i];
mx=max(mx, t);
}
} int main() {
read(n);
for1(i, 1, n) read(a[i]);
lis();
int tot=getint();
while(tot--) {
int m=getint();
if(m>mx) { puts("Impossible"); continue; }
int now=-oo;
for1(i, 1, n) if(m && f[i]>=m && a[i]>now) { printf("%d", a[i]); m==1?puts(""):printf(" "); --m; now=a[i]; } //注意特判m!=0
}
return 0;
}
Description
对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.
Input
第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M行每行一个数L,表示要询问长度为L的上升序列。
Output
对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.
Sample Input
3 4 1 2 3 6
3
6
4
5
Sample Output
1 2 3 6
Impossible
HINT
数据范围
N<=10000
M<=1000
Source
【BZOJ】1046: [HAOI2007]上升序列(dp)的更多相关文章
- bzoj 1046 : [HAOI2007]上升序列 dp
题目链接 1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3620 Solved: 1236[Submit] ...
- BZOJ 1046: [HAOI2007]上升序列 LIS -dp
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3438 Solved: 1171[Submit][Stat ...
- BZOJ 1046: [HAOI2007]上升序列【贪心+二分状态+dp+递归】
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4987 Solved: 1732[Submit][Stat ...
- 1046: [HAOI2007]上升序列(dp)
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4999 Solved: 1738[Submit][Stat ...
- Bzoj 1046: [HAOI2007]上升序列 二分,递推
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3671 Solved: 1255[Submit][Stat ...
- BZOJ 1046: [HAOI2007]上升序列(LIS)
题目挺坑的..但是不难.先反向做一次最长下降子序列.然后得到了d(i),以i为起点的最长上升子序列,接下来贪心,得到字典序最小. ----------------------------------- ...
- [BZOJ 1046] [HAOI2007] 上升序列 【DP】
题目链接:BZOJ - 1046 题目分析 先倒着做最长下降子序列,求出 f[i],即以 i 为起点向后的最长上升子序列长度. 注意题目要求的是 xi 的字典序最小,不是数值! 如果输入的 l 大于最 ...
- bzoj 1046: [HAOI2007]上升序列【dp+二分】
先从后到前做一个最长下降子序列的dp,记录f[i],我这里用的是二分(其实树状数组比较显然) 然后对于询问,超出最长上升子序列的直接输出:否则从前到后扫,f[i]>=x&&a[i ...
- bzoj 1046: [HAOI2007]上升序列
Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ...
- BZOJ 1046 [HAOI2007]上升序列(LIS + 贪心)
题意: m次询问,问下标最小字典序的长度为x的LIS是什么 n<=10000, m<=1000 思路: 先nlogn求出f[i]为以a[i]开头的LIS长度 然后贪心即可,复杂度nm 我们 ...
随机推荐
- 转:介绍shell_notifyicon,SendMessage,CallWindowProc,GetWindowLong,SetWindowLong的用法
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_NotifyIconA& ...
- Android获取手机位置代码实现
1.项目Src下创建...service包,然后新建GPSService类 package com.zebra.mobilesafe.service; import java.io.IOExcepti ...
- 如何下载flash离线安装包
如何下载flash离线安装包 CreateTime--2018年4月14日16:02:13 Author:Marydon 1.下载地址 UpdateTime--2018年5月13日16点55分 p ...
- java线程同步: synchronized详解(转)
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- 2、JSP脚本
JSP脚本 JSP脚本包含了JSP表达式.声明标识和脚本程序.通过这些标识,在JSP页面中可以如同编写Java程序一样来声明变量.定义方法和执行各种表达式的运算 1.在JSP中应用代码片段 语法格式: ...
- JDBC 事务(一) 隔离级别
public class TxTest { public static void main(String[] args) throws SQLException { test(); ...
- centos 6.5安装docker报错(查看报错详细信息--推荐)
(1)yum安装docker [root@namenode ~]# yum install docker-io (2)启动docker [root@namenode ~]# /etc/init.d/d ...
- 基于skitter的轮播图炫酷效果,幻灯片的体验
概述 包含各种炫酷的轮播切换效果,插件小巧,与其他插件无冲突,可用于移动端和PC端 详细 代码下载:http://www.demodashi.com/demo/11939.html 你还在用原生的js ...
- Mybatis-Generator自动生成代码
在使用mybatis开发的过程中,通常我们会给数据库的每张表编写对应的model.dao.mapping,虽然很简单,但是工作量很大,所以通常会使用代码生成器Mybatis-Generator帮我们自 ...
- AI的分支学科
AI 的分支学科 [References]AAI(Advanced Artificial Intelligence)