zoukankan      html  css  js  c++  java
  • bzoj 1046: [HAOI2007]上升序列

    Description

      对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax
    2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给
    出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先
    x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.

    Input

      第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M
    行每行一个数L,表示要询问长度为L的上升序列。N<=10000,M<=1000

    Output

      对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.

    Sample Input

    6
    3 4 1 2 3 6
    3
    6
    4
    5

    Sample Output

    Impossible
    1 2 3 6
    Impossible

    题解:

    古老的省选题...数据范围太小,可以乱来.

    因为要求编号的字典序越小,那么显然应该从前往后能选就选

    那么我们就反着做不上升子序列,最后每组询问O(n)扫一遍即可

    复杂度O(n*m)

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 using namespace std;
     8 const int N=10005,inf=2e8;
     9 int a[N],f[N],dis[N],n;
    10 int midit(int x){
    11     int l=0,r=n,mid,ret;
    12     while(l<=r){
    13         mid=(l+r)>>1;
    14         if(x<f[mid])ret=mid,l=mid+1;
    15         else r=mid-1;
    16     }
    17     return ret;
    18 }
    19 void work()
    20 {
    21     int tmp,ans=0;
    22     scanf("%d",&n);
    23     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    24     f[0]=inf;
    25     for(int i=n;i>=1;i--){
    26         tmp=midit(a[i])+1;
    27         if(a[i]>f[tmp])f[tmp]=a[i];
    28         dis[i]=tmp;
    29         if(tmp>ans)ans=tmp;
    30     }
    31     int m,x;
    32     scanf("%d",&m);
    33     while(m--){
    34         scanf("%d",&x);
    35         if(x>ans){
    36             printf("Impossible
    ");
    37             continue;
    38         }
    39         int last=0;
    40         for(int i=1;i<=n;i++){
    41             if(dis[i]>=x && a[i]>a[last]){
    42                 last=i;
    43                 printf("%d",a[i]);
    44                 x--;
    45                 if(x)putchar(' ');
    46                 else {
    47                     puts("");
    48                     break;
    49                 }
    50             }
    51         }
    52     }
    53 }
    54 
    55 int main()
    56 {
    57     work();
    58     return 0;
    59 }
  • 相关阅读:
    matlab的变量判断是字符还是数字
    《误杀2》影评
    木心诗选
    Matlab查找一个元素在向量或矩阵中的位置
    数据什么时候需要做中心化和标准化处理?
    The Elements of Statistical Learning
    matlab如何将一个矩阵的任意两行或两列交换
    三次多项式和三次样条曲线的区别
    redis如何设置密码
    阿里云LAMP 环境
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7301129.html
Copyright © 2011-2022 走看看