POJ 1631 nlogn求LIS
方法一: 二分
我们可以知道 最长上升子序列的 最后一个数的值是随序列的长度而递增的 (呃呃呃 意会意会)
然后我们就可以二分找值了(并更新)
//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的更多相关文章
- nlogn求LIS(树状数组)
之前一直是用二分 但是因为比较难理解,写的时候也容易忘记怎么写. 今天比赛讲评的时候讲了一种用树状数组求LIS的方法 (1)好理解,自然也好写(但代码量比二分的大) (2)扩展性强.这个解法顺带求出以 ...
- POJ 1631 Bridging signals (LIS:最长上升子序列)
题意:给你一个长为n(n<=40000)的整数序列, 要你求出该序列的最长上升子序列LIS. 思路:要求(nlogn)解法 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序 ...
- [POJ1631] nlogn求LIS
用到了algorithm自带的lower_bound函数进行二分查找 #include<cstdio> #include<cstring> #include<algori ...
- Codeforces 486E LIS of Sequence --树状数组求LIS
题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i] ...
- poj1631——树状数组求LIS
题目:http://poj.org/problem?id=1631 求LIS即可,我使用了树状数组. 代码如下: #include<iostream> #include<cstdio ...
- hdu 1025LIS思路同1257 二分求LIS
题目: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- HDU 5773 The All-purpose Zero(O(nlgn)求LIS)
http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...
- UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)
题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...
- 【模板】O(nlongn)求LIS
合理运用单调性降低复杂度 平常用的都是O(n^2)的dp求LIS(最长不下降子序列)这里介绍O(nlogn)的算法 分析 对于可能出现的x<y<i且A[y]<A[x]<A[i] ...
随机推荐
- C++实现页码数字统计
#include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #incl ...
- weblogic部署struts2项目訪问action404错误
近期有个project部署到tomcat上是正常的,部署到weblogic上时訪问action报404错误.依据报错日志.在网上找到了原因例如以下: 部署到weblogic上.struts.xml配置 ...
- CCControlExtension/CCControlPotentiometer
#ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...
- Medium上的文章
Welcome to Medium, a place to read, write, and interact with the stories that matter most to you. 网站 ...
- POJ 3044单调栈
题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...
- css3 -阻止元素成为鼠标事件目标 pointer-events
pointer-events:auto|none 其中pointer-events:none:元素永远不会成为鼠标事件的target. <!DOCTYPE html> <html l ...
- jq不懂的地方
在循环列表中,获取input标签的值,不能用id获取,用class获取值,通过父级属性找到class,this 指当前点击的位置var UID = $(this).parents("tr&q ...
- Spring jar包功能
1.spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.spring ...
- CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
题目链接:https://cn.vjudge.net/problem/CodeForces-920E 题意 给一个补图,问各个联通块有几个元素,升序排列 注意maxn=2e5, maxm=2e10 思 ...
- 紫书 习题8-12 UVa 1153(贪心)
本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...