hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748
树状数组:
/*
对于普通的LIS:
for(i):1~n LIS[i]=1;
if j<i and a[j]<a[i]
LIS[i]=LIS[j]+1
因此可知LIS转移需要两个条件
1.(j<i) 序号必须在i之前
2.(a[i]>a[j]) 值必须比a[i]小
利用树状数组的顺序操作:{查找的都是已经出现的,序号在前(满足条件1)}
对于每一个值,查找它在数组中的排名,再去寻找小于它的排名的最大的LIS(满足条件2)
这里利用到了排名,因为这样可以最大限度地压缩C数组的空间
*/
#include <bits/stdc++.h>
using namespace std;
const int Max=1e5+;
int A[Max],V[Max],L[Max],C[Max],len;
int lowbit(int x) {return x&(-x);}
int Sum(int x) //求值小于等于x的LIS的最大值
{
int ret=;
while(x>)
{
if(C[x]>ret) ret=C[x];
x-=lowbit(x);
}
return ret;
}
void Add(int x,int d) //值大于等于x的LIS都改为LIS(x)
{
while(x<=len)
{
if(d>C[x]) C[x]=d;
x+=lowbit(x);
}
}
int main()
{
int T;
for(scanf("%d",&T);T;T--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&A[i]);
V[i]=A[i];
}
sort(V+,V++n);
len=unique(V+,V++n)-(V+);
memset(C,,sizeof(C));
int ans=,tmp,xu;
for(int i=;i<=n;i++)
{
xu=lower_bound(V+,V++len,A[i])-(V);
tmp=Sum(xu-)+;
L[i]=tmp;
Add(xu,tmp);
}
for(int i=;i<=n;i++)
{
if(i!=) printf(" ");
printf("%d",L[i]);
}
puts("");
}
return ;
}
dp+二分
/*
以dp[x]代表长度为x的LIS,且dp[x]==LIS长度为x的末尾值
每次都往前取dp[x]中最小的一个,当然在保证x尽可能地大的情况下
因为dp[x]是递增的,所以可以二分,l=1,r=当前最长的LIS
求得当前以小于当前a[i]的最长LIS
*/
#include <bits/stdc++.h>
using namespace std;
const int Max=1e5+;
int A[Max];
int dp[Max];
int LIS[Max];
void Get_lis(int n)
{
int i,j,l,r,mid,ans;
dp[]=A[];
int len=;
for(i=;i<=n;i++)
{
if(dp[len]<A[i]) j=++len;
else
{
l=;r=len;
ans=;
while(l<=r)
{
mid=(l+r)>>;
if(A[i]>dp[mid]&&A[i]<=dp[mid+])
{
ans=mid;break;
}
else if(A[i]>dp[mid]) l=mid+;
else r=mid-;
}
j=ans+;
}
dp[j]=A[i];
LIS[i]=j;
}
}
int main()
{
int T;
for(scanf("%d",&T);T;T--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&A[i]);
dp[i]=;
}
LIS[]=;
Get_lis(n);
for(int i=;i<=n;i++)
{
if(i!=) printf(" ");
printf("%d",LIS[i]);
}
puts("");
}
return ;
}
其实还有一种单调队列求最长上升子序列的方法,可是不能用来解这道题
/*
无解。。。
单调队列只能求出总体的LIS长度
*/
#include <bits/stdc++.h>
using namespace std;
const int Max=1e5+;
int que[Max];
int main()
{
int T;
for(scanf("%d",&T);T;T--)
{
int n,x,top=;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&x);
if(x>que[top]||top==)
{
que[++top]=x;
}
else
{
int l=,r=top,mid,ans;
ans=;
while(l<=r)
{
mid=l+(r-l)/;
if(que[mid]<x) l=mid+;
else r=mid-,ans=mid;
}
que[ans]=x;
}
}
cout<<top<<endl;
}
return ;
}
hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)的更多相关文章
- 求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法
1.题目描述 给定数组arr,返回arr的最长递增子序列. 2.举例 arr={2,1,5,3,6,4,8,9,7},返回的最长递增子序列为{1,3,4,8,9}. 3.解答 ...
- HDU 4681 String 最长公共子序列
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- hdu 5489(LIS最长上升子序列)
题意:一个含有n个元素的数组,删去k个连续数后,最长上升子序列 /*思路参考GoZy 思路: 4 2 3 [5 7 8] 9 11 ,括号表示要删掉的数, 所以 最长上升子序列 = ...
- hdu 5532(最长上升子序列)
Input The first line contains an integer T indicating the total number of test cases. Each test case ...
- Python动态规划求解最长递增子序列(LIS)
原始代码错误,移步博客查看O(N^2)及优化的O(N*logN)的实现:每天一道编程题--最长递增子序列
- 算法练习--- DP 求解最长上升子序列(LIS)
问题描写叙述: 对于2,5,3,1,9,4,6,8,7,找出最长上升子序列的个数 最长上升子序列定义: 对于i<j i,j∈a[0...n] 满足a[i]<a[j] 1. 找出DP公式:d ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 最长上升子序列算法(n^2 及 nlogn) (LIS) POJ2533Longest Ordered Subsequence
问题描述: 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列 ...
随机推荐
- Linux ToolChain (二) --- Linker (1)链接选项 -L -rpath -rpath-link
一.动态库的链接和链接选项-L,-rpath-link,-rpath (1). 现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开, 用 ...
- 2016/12/14---- C3P0
查询查询一条记录 public UserBean findActiver(String ac) throws SQLException { QueryRunner qr = new QueryRunn ...
- 特殊的Josn格式
static void Main(string[] args) { YtRequest<RequestHead, RequestBody> Ytrequ ...
- ios delegate, block, NSNotification用法
ios中实现callback可以通过两种方法,委托和NSNotification 委托的话是一对一的关系,例如一个UIViewController里有一个tableView, 将该viewContro ...
- free pascal 错误代码表
free pascal 错误代码表 为了方便对照检查运行时错误代码,这里把所有的错误代码的含义整理出来.(最大序号为232,中间不一定连续.user.pdf P175) Run-time errors ...
- VC++ chap12 file
file operation _______C语言对文件操作的支持 fopen accepts paths that are valid on the file system at the point ...
- Linux内核启动logo
之前在分析samsung的fb驱动代码的时候,其中有一段代码是处理内核logo显示相关的,今天就内核logo这个话题来聊一聊! 一.处理内核logo显示相关的代码在哪? 回到samsung的fb驱动代 ...
- __new__
[__new__] object.__new__(cls[, ...]) Called to create a new instance of class cls. 用于创建类对象cls的实例. __ ...
- sublimetext
下载地址:http://www.sublimetext.com/ 详情:http://baike.baidu.com/link?url=uoObJWXyy_-zu52HuOKzfKuwHEpL2JQn ...
- oracle数据学习第二天
今天主要加强了对oracle数据库的数据类型一些相关函数的学习 (一)char和varchar2 字符串函数 <1>concat(char1,char2)字符串连接函数,用于连接两个字 ...