方法一: 二分

我们可以知道 最长上升子序列的 最后一个数的值是随序列的长度而递增的 (呃呃呃 意会意会)

然后我们就可以二分找值了(并更新)

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,cases,a[100050],f[100050],vis[100050];
int search(int x){
int l=0,r=n,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(vis[mid]<=x)l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
int main()
{
scanf("%d",&cases);
while(cases--){
memset(f,0,sizeof(f));
memset(vis,0x3f,sizeof(vis)),vis[0]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
f[i]=search(a[i])+1;
vis[f[i]]=min(vis[f[i]],a[i]);
}
for(int i=1;i<n;i++)f[n]=max(f[n],f[i]);
printf("%d\n",f[n]);
}
}

方法二:

线段树 (按照权值建) 查询前一段中的最大值。。并插入当前的值,,

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,cases,a[100050],f[100050],xx,tree[666666];
void insert(int l,int r,int pos){
if(l==r){tree[pos]=f[xx];return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid<a[xx])insert(mid+1,r,rson);
else insert(l,mid,lson);
tree[pos]=max(tree[lson],tree[rson]);
}
int query(int l,int r,int pos){
if(r<=a[xx])return tree[pos];
int mid=(l+r)>>1;
if(mid<a[xx])return max(query(l,mid,pos<<1),query(mid+1,r,pos<<1|1));
else return query(l,mid,pos<<1);
}
int main(){
scanf("%d",&cases);
while(cases--){
memset(tree,0,sizeof(tree));
memset(f,0,sizeof(f));
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(xx=1;xx<=n;xx++){
f[xx]=query(1,n,1)+1;
insert(1,n,1);
}
for(int i=1;i<n;i++)f[n]=max(f[n],f[i]);
printf("%d\n",f[n]);
}
}

POJ 1631 nlogn求LIS的更多相关文章

  1. nlogn求LIS(树状数组)

    之前一直是用二分 但是因为比较难理解,写的时候也容易忘记怎么写. 今天比赛讲评的时候讲了一种用树状数组求LIS的方法 (1)好理解,自然也好写(但代码量比二分的大) (2)扩展性强.这个解法顺带求出以 ...

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

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

  3. [POJ1631] nlogn求LIS

    用到了algorithm自带的lower_bound函数进行二分查找 #include<cstdio> #include<cstring> #include<algori ...

  4. Codeforces 486E LIS of Sequence --树状数组求LIS

    题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i] ...

  5. poj1631——树状数组求LIS

    题目:http://poj.org/problem?id=1631 求LIS即可,我使用了树状数组. 代码如下: #include<iostream> #include<cstdio ...

  6. hdu 1025LIS思路同1257 二分求LIS

    题目: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  7. HDU 5773 The All-purpose Zero(O(nlgn)求LIS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...

  8. UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)

    题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...

  9. 【模板】O(nlongn)求LIS

    合理运用单调性降低复杂度 平常用的都是O(n^2)的dp求LIS(最长不下降子序列)这里介绍O(nlogn)的算法 分析 对于可能出现的x<y<i且A[y]<A[x]<A[i] ...

随机推荐

  1. fork同一时候创建多个子进程的方法

    Fork同一时候创建多个子进程方法 第一种方法:验证通过 特点:同一时候创建多个子进程.每一个子进程能够运行不同的任务,程序 可读性较好,便于分析,易扩展为多个子进程 int main(void) { ...

  2. 110_leetcode_Best Time to Buy and sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. Stop being a perfectionist

    节选自 7 Things You Need To Stop Doing To Be More Productive, Backed By Science “We found that perfecti ...

  4. 去除iframe滚动条1

    主页面的IFRAME中添加:scrolling="yes" 子页面程序代码: 让竖条消失: <body style='overflow:scroll;overflow-x:a ...

  5. zzulioj--1712--神秘的数列(水题)

    1712: 神密的数列 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 92 SubmitStatusWeb Board De ...

  6. su和sudo的区别与使用,su命令,linux命令

    su和sudo的区别与使用 一.   使用 su 命令临时切换用户身份 1. su 的适用条件和威力 su命令就是切换用户 的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务, ...

  7. 13.boost有向无向图邻接表表示

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  8. xBIM 基础04 日志操作

    系列目录    [已更新最新开发文章,点击查看详细]  xBIM使用Log4Net来记录异常不适合的处理错误和警告.它在xBIM几何引擎中被大量使用来记录所有几何错误.如果您的文件看起来不太正确,则应 ...

  9. [原创]微信小程序 实现 圆环 百分百效果

    1.最终效果 2.技术点:a. css3 clip-path , b.根据角度和直边计算另一个直边的长度 3.实现思路: a.3个层(灰色圆形层, 红色圆形层,白色圆形层)  ,其中灰色和红色层大小一 ...

  10. Sublime Text 3 注册码 激活码 版本号 Build 3143

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...