题意:

  给一个数字序列,要求找到LIS,输出其长度。

思路:

  扫一遍+二分,复杂度O(nlogn),空间复杂度O(n)。

  具体方法:增加一个数组,用d[i]表示长度为 i 的递增子序列的最后一个元素,且该元素总是保持当前最小。初始化d[1]=A[i],当前LIS的长度len=1。从 2 to n,若A[i]>d[len],则d[++len]=A[i],否则,在数组d中找到A[i]应该插入的位置,代替掉那个第一个比它大的数字,比如d[k]<A[i]<=d[k+1],直接将A[i]代替掉d[k+1]。完成后len就是LIS的长度了。

 #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 2147483647
#define LL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int mod=1e9+;
int a[N], d[N];
int* lower_( int *s,int *e,int val ) //二分找值,返回下标
{
int L=, R=e-s-, mid;
while(L<R)
{
mid=R-(R-L+)/; //保证至少减少1
if( s[mid]<val ) L=mid+;//至少增加1
else R=mid;
}
return &s[R];
} int main()
{
freopen("input.txt", "r", stdin);
int t, n, len;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++) scanf("%d",&a[i]);
len=;
d[len]=a[]; for( int i=; i<=n; i++ )
{
if( a[i]>d[len] ) d[++len]=a[i];
else *lower_(d+,d+len+,a[i])=a[i];
//else *lower_bound(d+1,d+len+1,a[i])=a[i]; 上一行代码可换成此行
}
printf("%d\n",len);
}
return ;
}

AC代码

HDU 1950 Bridging signals (LIS,O(nlogn))的更多相关文章

  1. [POJ1631]Bridging signals (DP,二分优化)

    题目链接:http://poj.org/problem?id=1631 就是求一个LIS,但是范围太大(n≤40000),无法用常规O(n²)的朴素DP算法,这时需要优化. 新加一个数组s[]来维护长 ...

  2. POJ 1631 Bridging signals(LIS 二分法 高速方法)

    Language: Default Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1076 ...

  3. (hdu)1950 Bridging signals(最长上升子序列)

    Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...

  4. POJ 1631 Bridging signals(LIS的等价表述)

    把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...

  5. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  6. HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...

  7. HDU 3338 Kakuro Extension (网络流,最大流)

    HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...

  8. HDU 4280 Island Transport(网络流,最大流)

    HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...

  9. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...

随机推荐

  1. linux命令:rsync

    Rsync的命令格式可以为以下六种: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]. ...

  2. 华为CodeCraft2018 周进展

    上周: python验证lstm,效果不好.很多拟合的是直线.C++抄了个lstm,输出也是直线,不知道是程序的问题,还是模型的问题. 尝试bp神经网络求解.代码是抄的.回看天数是写死的,隐层只有一层 ...

  3. python 中 模块,包, 与常用模块

    一 模块 模块:就是一组功能的集合体, 我们的程序可以直接导入模块来复用模块里的功能 导入方式 一般为 : import 模块名 在python中, 模块一般分为四个通用类别 1使用python编写. ...

  4. E20181121-hm

    invoke vt. 乞灵,祈求; 提出或授引…以支持或证明; 召鬼; 借助

  5. E20180511-hm

    thread  n. 螺纹; 线; 线索; 线状物;      vt. 穿成串; 将(针.线等)穿过…; 用…线缝; 给…装入(胶片.狭带.绳子); needle n. 针; 针状物; <口&g ...

  6. Swift3.0 键盘高度监听获取

    方法:通过通知监听键盘的动态 1.键盘的动态有四种: public static let UIKeyboardWillShow: NSNotification.Name public static l ...

  7. Linux下新建一个站点

    Apache+nagix使用Lnmpa创建一个新的站点 我们在部署服务器的时候通常会遇到需要分域名和分应用部署,那么如何通过Apache+nagix创建一个新的站点服务呢 LNMPA这种架构有什么优势 ...

  8. Forward Rendering 正向渲染

    Forward Rendering 正向渲染        正向渲染一个基于着色器的渲染路径.它支持逐像素计算光照(包括法线贴图和灯光Cookies)和来自一个平行光的实时阴影.在默认设置中,少数最亮 ...

  9. 第十九篇 .NET高级技术之C#中的线程(一)

    原文://http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html 文章系参考转载,英文原文网址请参考:http://www.al ...

  10. time库的使用

    首先只需要 import time (典型的,标准的python库的使用方法) 主要包括三类函数 ——时间获取:time() , ctime() , gmtime() ——时间格式化: strftim ...