题目大意:求最长上升子序列(LIS)长度,序列最大数不超过40000。因为只有上升排列的接口才不相交。

思路:普通的 O(n^2)的做法肯定会超时;因此,dp[ ] 记录长度为 i+1 的子序列中最末元素的最小值,这一数组是单调递增的,因此对于dp[ ]数组内元素可以用二分搜索找出dp[ ]中比 a[ i ] 大的最小的元素的位置;这里用到了STL类里的 lower_bound(x, x+n, k)函数(http://www.cplusplus.com/reference/algorithm/lower_bound/?kw=lower_bound)函数返回排好序的序列
x[ ] 中满足 x[ i ]  >= k  的 x[ i ] 的最小指针。

AC代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define M 40010
#define INF 1000000
int dp[M],a[M];
void init(int *a, int n)
{
for(int i = 0; i < n; i++)
{
a[i] = INF;
}
} int main()
{
int n;
scanf("%d",&n) == 1;
while(n--)
{
int x;
scanf("%d",&x);
for(int i = 0; i < x; i++)
scanf("%d",&a[i]);
init(dp,x);
for(int i = 0; i < x; i++)
*lower_bound(dp, dp+x, a[i]) = a[i];
cout<<lower_bound(dp, dp+x, INF) - dp<<endl;
}
return 0;
}
作者:u011652573 发表于2014-3-6 14:58:03 原文链接
阅读:51 评论:0 查看评论

[原]POJ-1631-Bridging signals-( 水LIS-O(nlogn) -DP)的更多相关文章

  1. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

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

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

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

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

  4. OpenJudge/Poj 1631 Bridging signals

    1.链接地址: http://poj.org/problem?id=1631 http://bailian.openjudge.cn/practice/1631 2.题目: Bridging sign ...

  5. POJ 1631 Bridging signals

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9441   Accepted: 5166 ...

  6. poj 1631 Bridging signals (二分||DP||最长递增子序列)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9234   Accepted: 5037 ...

  7. POJ 1631 Bridging signals (LIS:最长上升子序列)

    题意:给你一个长为n(n<=40000)的整数序列, 要你求出该序列的最长上升子序列LIS. 思路:要求(nlogn)解法 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序 ...

  8. Poj 1631 Bridging signals(二分+DP 解 LIS)

    题意:题目很难懂,题意很简单,求最长递增子序列LIS. 分析:本题的最大数据40000,多个case.用基础的O(N^2)动态规划求解是超时,采用O(n*log2n)的二分查找加速的改进型DP后AC了 ...

  9. POJ - 1631 Bridging signals(最长上升子序列---LIS)

    题意:左右各n个端口,已知n组线路,要求切除最少的线路,使剩下的线路各不相交,按照左端口递增的顺序输入. 分析: 1.设左端口为l,右端口为r,因为左端口递增输入,l[i] < l[j](i & ...

  10. POJ 1631 Bridging signals & 2533 Longest Ordered Subsequence

    两个都是最长上升子序列,所以就放一起了 1631 因为长度为40000,所以要用O(nlogn)的算法,其实就是另用一个数组c来存储当前最长子序列每一位的最小值,然后二分查找当前值在其中的位置:如果当 ...

随机推荐

  1. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  2. 图解Git/图形化的Git参考手册

    此页图解git中的最常用命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 上面的四条命令在工作目录.暂存目录(也叫做索引)和仓库之间复制文件. ● git add fil ...

  3. 【CentOs】sudo使用

    在使用Linux系统过程中,通常情况下,我们都会使用普通用户进行日常操作,而root用户只有在权限分配及系统设置时才会使用,而root用户的密码也不可能公开.普通用户执行到系统程序时,需要临时提升权限 ...

  4. Winform跨线程操作界面的策略

    BeginInvoke(new ThreadStart(() => toolStripButton1.Text = "aaa")); 1.非跨线程操作和部分跨线程get不会引 ...

  5. 讨论下IDS的绕过

    自从知道dedecms自带了80sec的内置Mysqlids后,一直以来也没有想到绕过的办法.或者是自己mysql的根底太差了吧.于是分析dedecms源码时,只找模板执行,本地包含,上传等,完全没有 ...

  6. HTTP persistent connection

    http://en.wikipedia.org/wiki/HTTP_persistent_connection

  7. c#比较器 排序

    原地址:http://blog.csdn.net/xutao_ustc/article/details/6314057 class Program { static void Main(string[ ...

  8. 【面试题】Round A China New Grad Test 2014总结

    我也有够懒的,今天才跑来写总结,自觉面壁中… 上一篇是Practice Round,今天是Round A,五道题. 每次做完都想说,其实题不难..但在做的过程中总是会各种卡,只有自己一行一行实现了,才 ...

  9. css系列-段落首字符下沉、缩进及特殊显示

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 深入浅出ES6(十七):展望未来

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 出于对文章长度的考虑,我们还保留了一些尚未提及的新特性,在最后的这篇文章中我会集 ...