【BZOJ 1046】[HAOI2007]上升序列 lis
2016-11-06 20:06
381 查看
额,一开始没有认真读题,我以为最小字典序输出是数字大小最小字典序,结果是位置,我靠水我呢。
倒过来做一次最长下降子序列(手顺一下子写成最长不上升子序列,一个元素有重复,一个没有重复),得到重每一个位置开始的最长上升子序列(就是倒过来的最长下降),输出的时候贪心,能输出就直接输出
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[20200],cnt[20300],c[20200],tot;
int find(int x){
int l=1,r=tot;
while(l<r){
int mid=l+r>>1;
if(c[mid]>x)l=mid+1;
else r=mid;
}
return l;
}
void make(){
for(int x,i=n;i>=1;i--){
if(tot==0||a[i]<c[tot])c[++tot]=a[i],cnt[i]=tot;
else {
x=find(a[i]);
c[x]=a[i];
cnt[i]=x;
}
}
}
void Print(int x){
int last=-1;
for(int i=1;i<=n;i++){
if(cnt[i]>=x&&a[i]>last){
x--;
printf("%d%c",a[i],x?' ':'\n'),last=a[i];
}
if(x==0)break;
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
make();int x;
scanf("%d",&m);
while(m--){
scanf("%d",&x);
if(x>tot)puts("Impossible");
else Print(x);
}
return 0;
}
倒过来做一次最长下降子序列(手顺一下子写成最长不上升子序列,一个元素有重复,一个没有重复),得到重每一个位置开始的最长上升子序列(就是倒过来的最长下降),输出的时候贪心,能输出就直接输出
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[20200],cnt[20300],c[20200],tot;
int find(int x){
int l=1,r=tot;
while(l<r){
int mid=l+r>>1;
if(c[mid]>x)l=mid+1;
else r=mid;
}
return l;
}
void make(){
for(int x,i=n;i>=1;i--){
if(tot==0||a[i]<c[tot])c[++tot]=a[i],cnt[i]=tot;
else {
x=find(a[i]);
c[x]=a[i];
cnt[i]=x;
}
}
}
void Print(int x){
int last=-1;
for(int i=1;i<=n;i++){
if(cnt[i]>=x&&a[i]>last){
x--;
printf("%d%c",a[i],x?' ':'\n'),last=a[i];
}
if(x==0)break;
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
make();int x;
scanf("%d",&m);
while(m--){
scanf("%d",&x);
if(x>tot)puts("Impossible");
else Print(x);
}
return 0;
}
相关文章推荐
- bzoj 1046: [HAOI2007]上升序列 LIS
- BZOJ 1046: [HAOI2007]上升序列(LIS)
- BZOJ 1046: [HAOI2007]上升序列 LIS -dp
- 【LIS】BZOJ1046(HAOI2007)[上升序列]题解
- BZOJ 1046([HAOI2007]上升序列-LIS二分O(nlogn))
- BZOJ 1046: [HAOI2007]上升序列 LIS
- [BZOJ 1046][HAOI 2007]上升序列(nlogn的LIS算法)
- BZOJ1046 [HAOI2007]上升序列 【LIS + 字典序最小】
- 【BZOJ】1046: [HAOI2007]上升序列(dp)
- BZOJ 1046: [HAOI2007]上升序列
- BZOJ1046 [HAOI2007]上升序列 【LIS + 字典序最小】
- [bzoj1046][HAOI2007]上升序列【dp】
- BZOJ 1046: [HAOI2007]上升序列
- Bzoj1046: [HAOI2007]上升序列
- BZOJ1046: [HAOI2007]上升序列
- 【bzoj1046】 HAOI2007—上升序列
- 【动态规划】【最长上升子序列】【贪心】bzoj1046 [HAOI2007]上升序列
- BZOJ 1046 [HAOI2007]上升序列
- [bzoj1046][HAOI2007]上升序列
- BZOJ 1046: [HAOI2007]上升序列 暴力,二分