方法一: 二分

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

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

  1. //By SiriusRen
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. int n,cases,a[100050],f[100050],vis[100050];
  7. int search(int x){
  8. int l=0,r=n,ans=0;
  9. while(l<=r){
  10. int mid=(l+r)>>1;
  11. if(vis[mid]<=x)l=mid+1,ans=mid;
  12. else r=mid-1;
  13. }
  14. return ans;
  15. }
  16. int main()
  17. {
  18. scanf("%d",&cases);
  19. while(cases--){
  20. memset(f,0,sizeof(f));
  21. memset(vis,0x3f,sizeof(vis)),vis[0]=0;
  22. scanf("%d",&n);
  23. for(int i=1;i<=n;i++)
  24. scanf("%d",&a[i]);
  25. for(int i=1;i<=n;i++){
  26. f[i]=search(a[i])+1;
  27. vis[f[i]]=min(vis[f[i]],a[i]);
  28. }
  29. for(int i=1;i<n;i++)f[n]=max(f[n],f[i]);
  30. printf("%d\n",f[n]);
  31. }
  32. }

方法二:

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

  1. //By SiriusRen
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. int n,cases,a[100050],f[100050],xx,tree[666666];
  7. void insert(int l,int r,int pos){
  8. if(l==r){tree[pos]=f[xx];return;}
  9. int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
  10. if(mid<a[xx])insert(mid+1,r,rson);
  11. else insert(l,mid,lson);
  12. tree[pos]=max(tree[lson],tree[rson]);
  13. }
  14. int query(int l,int r,int pos){
  15. if(r<=a[xx])return tree[pos];
  16. int mid=(l+r)>>1;
  17. if(mid<a[xx])return max(query(l,mid,pos<<1),query(mid+1,r,pos<<1|1));
  18. else return query(l,mid,pos<<1);
  19. }
  20. int main(){
  21. scanf("%d",&cases);
  22. while(cases--){
  23. memset(tree,0,sizeof(tree));
  24. memset(f,0,sizeof(f));
  25. scanf("%d",&n);
  26. for(int i=1;i<=n;i++)
  27. scanf("%d",&a[i]);
  28. for(xx=1;xx<=n;xx++){
  29. f[xx]=query(1,n,1)+1;
  30. insert(1,n,1);
  31. }
  32. for(int i=1;i<n;i++)f[n]=max(f[n],f[i]);
  33. printf("%d\n",f[n]);
  34. }
  35. }

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. C++实现页码数字统计

    #include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #incl ...

  2. weblogic部署struts2项目訪问action404错误

    近期有个project部署到tomcat上是正常的,部署到weblogic上时訪问action报404错误.依据报错日志.在网上找到了原因例如以下: 部署到weblogic上.struts.xml配置 ...

  3. CCControlExtension/CCControlPotentiometer

    #ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...

  4. Medium上的文章

    Welcome to Medium, a place to read, write, and interact with the stories that matter most to you. 网站 ...

  5. POJ 3044单调栈

    题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...

  6. css3 -阻止元素成为鼠标事件目标 pointer-events

    pointer-events:auto|none 其中pointer-events:none:元素永远不会成为鼠标事件的target. <!DOCTYPE html> <html l ...

  7. jq不懂的地方

    在循环列表中,获取input标签的值,不能用id获取,用class获取值,通过父级属性找到class,this 指当前点击的位置var UID = $(this).parents("tr&q ...

  8. Spring jar包功能

    1.spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.spring ...

  9. CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除

    题目链接:https://cn.vjudge.net/problem/CodeForces-920E 题意 给一个补图,问各个联通块有几个元素,升序排列 注意maxn=2e5, maxm=2e10 思 ...

  10. 紫书 习题8-12 UVa 1153(贪心)

    本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...