zoukankan      html  css  js  c++  java
  • [BZOJ1046] [HAOI2007] 上升序列 (dp)

    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

    HINT

    Source

    Solution

      先求出以每个数开头的最长上升子序列长度,然后从左往右贪心判断这个数选不选

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a[10005], b[10005], f[10005], ans[10005];
     4 int main()
     5 {
     6     int n, m, l, r, maxl = 0;
     7     scanf("%d", &n);
     8     for(int i = 1; i <= n; ++i)
     9         scanf("%d", a + i);
    10     a[0] = 2147483647, ans[0] = -2147483648;
    11     for(int i = n; i; --i)
    12     {
    13         l = 0, r = maxl + 1;
    14         while(l < r - 1)
    15         {
    16             int mid = (l + r) >> 1;
    17             if(a[b[mid]] > a[i]) l = mid;
    18             else r = mid;
    19         }
    20         f[i] = ++l, b[r] = i;
    21         if(l > maxl) ++maxl;
    22     }
    23     scanf("%d", &m);
    24     while(m--)
    25     {
    26         scanf("%d", &l);
    27         if(l > maxl) puts("Impossible");
    28         else
    29         {
    30             int cnt = l;
    31             for(int i = 1; i <= n && cnt; ++i)
    32                 if(f[i] >= cnt && a[i] > ans[l - cnt])
    33                     --cnt, ans[l - cnt] = a[i];
    34             for(int i = 1; i <= l; ++i)
    35                 if(i == 1) printf("%d", ans[i]);
    36                 else printf(" %d", ans[i]);
    37             puts("");
    38         }
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    angularjs 表单验证
    (原)Eclipse Tomcat配置(2014.12.27——By小赞)
    Eclipse SVN插件安装与使用(2014.12.27——by小赞)
    Eclipse 下载与安装(2014.12.26——by小赞)
    MySQL开启远程链接(2014.12.12)
    360手机助手使用问题
    WORD文档的长串数字如何粘贴到excel
    EJB 总结学习(1)
    EJB (not bound)
    JQ绑定事件(1.9已经废除了live()等绑定事件方法,on()方法是官方推荐的绑定事件的一个方法)
  • 原文地址:https://www.cnblogs.com/CtrlCV/p/5671653.html
Copyright © 2011-2022 走看看