N - 导弹拦截

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都要高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭,并观测到导弹依次飞来的高度,请计算这套系统最多能拦截多少导弹,同时,司令部想知道拦截下来的导弹的高度。拦截来袭导弹时,必须按来袭导弹袭击的时间顺序,不允许先拦截后面的导弹,再拦截前面的导弹。

Input

第一行是一个整数t,代表case数。 对于每一个case,第一行是一个整数n(1≤n≤100000); 第二行是n个非负整数,表示第n枚导弹的高度,按来袭导弹的袭击时间顺序给出,以空格分隔。数据保证高度不会超过100000.

Output

对于每一个case,第一行输出最多能拦截的导弹数,第二行按来袭顺序输出拦截下来的导弹的高度构成的序列,以一个空格隔开。若有不止一种方法可以拦截最多的导弹,输出字典序最小的。

Sample input and output

Sample Input Sample Output
1
5
1 6 3 5 7
4
1 3 5 7

解题报告:

首先我们令 c[i] 表示长度为 i 的上升子序列中最后一位最小的.可以注意到,c数组是单调不减的,我们每次都可二分求解.

之后我们考虑如何输出答案,从后往前扫,依次匹配长度为 x ,x – 1 , x – 2 ….为结尾的上升子序列,每次需要前面一个数小于后面一个数即可.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 1e5 + ;
int h[maxn],n,c[maxn],maxlength,f[maxn];
stack<int>s;
/*
贪心dp
c[i] -> 长度为 i 的最长上升子序列中的最小的最后一位
*/ int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
c[] = ;
while(Case--)
{
memset(c,-,sizeof(c));
scanf("%d",&n);
for(int i = ; i < n ; ++ i ) scanf("%d",h + i);
c[] = h[] , maxlength = , f[] = ;
for(int i = ; i < n ; ++ i)
{
int pos = lower_bound(c+,c+maxlength+,h[i]) - c;
if (c[pos] == - || c[pos] > h[i])
c[pos] = h[i];
f[i] = pos;
maxlength = max(maxlength,f[i]);
}
printf("%d\n",maxlength);
int need = maxlength , pre = << ;
for(int i = n - ; i >= ; -- i) //遍历 ,尽可能小
{
if (f[i] == need && h[i] < pre)
{
need--;
pre = h[i];
s.push(h[i]);
}
}
printf("%d",s.top());s.pop();
while(!s.empty())
{
printf(" %d",s.top());
s.pop();
}
printf("\n");
}
return ;
}

UESTC_导弹拦截 2015 UESTC Training for Dynamic Programming<Problem N>的更多相关文章

  1. UESTC_摩天轮 2015 UESTC Training for Dynamic Programming<Problem K>

    K - 摩天轮 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submi ...

  2. UESTC_最少花费 2015 UESTC Training for Dynamic Programming<Problem D>

    D - 最少花费 Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. UESTC_酱神寻宝 2015 UESTC Training for Dynamic Programming<Problem O>

    O - 酱神寻宝 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  4. UESTC_酱神的旅行 2015 UESTC Training for Dynamic Programming<Problem M>

    M - 酱神的旅行 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

  5. UESTC_菲波拉契数制升级版 2015 UESTC Training for Dynamic Programming<Problem L>

    L - 菲波拉契数制升级版 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  6. UESTC_男神的约会 2015 UESTC Training for Dynamic Programming<Problem J>

    J - 男神的约会 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

  7. UESTC_邱老师选妹子(二) 2015 UESTC Training for Dynamic Programming<Problem I>

    I - 邱老师选妹子(二) Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  8. UESTC_邱老师选妹子 2015 UESTC Training for Dynamic Programming<Problem H>

    H - 邱老师选妹子 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  9. UESTC_邱老师玩游戏 2015 UESTC Training for Dynamic Programming<Problem G>

    G - 邱老师玩游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

随机推荐

  1. SxsTrace工具使用方法

    Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库的版本和路径.   SxsTrace使用的方法:   1.首先必须以Administrator用户身份登录 ...

  2. 一篇非常经典的springMVC注解实现方式详解

    今天公司让搭建个springMVC的注解框架,研究了好半天,网络搜罗了半天,好不容易找到篇,拿来分享下: 原文出处:http://itxxz.com/a/kuangjia/2014/0531/4.ht ...

  3. SQL查询最近三个月的数据(查询最近几天,几年等等)

    定义和用法      :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId    OrderPayD ...

  4. 五分钟读懂UML类图

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  5. poj 1088 滑雪问题

    滑雪问题 import java.util.Scanner; public class Main{ static int a[][],r,c; public static void main(Stri ...

  6. uva 11210 Chinese Mahjong(暴力搜索)

    Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles res ...

  7. 【HDU】 1018 Big Number

    大意就是求 : log10(n!) = log10(1 * 2 *  3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...

  8. win下 git gui 使用教程

    现在很多都有git来托管项目或者来查找资料,但是看起来操作不是很方便,现在由于win下可以直接使用git gui,让使用git变得方便,当然这只是针对日常简单的使用,如果想详细的使用,可以去参考廖学峰 ...

  9. css中使用rgba和opacity设置透明度的区别

    1.使用rgba设置背景色的透明 效果如下: <body> <div id="box"> 你好啊! </div> </body> b ...

  10. mono for android工具下载

    http://www.wuleba.com/25510.html Windows平台:http://xamarin.com/installer_assets/v3/Windows/Universal/ ...