hdu 1950 最长上升子序列(lis) nlogn算法【dp】
这个博客说的已经很好了。http://blog.csdn.net/shuangde800/article/details/7474903
简单记录一下自己学的:
问题就是求一个数列最长上升子序列的长度。
如果子序列长度相同,那么末尾小的子序列更有可能成为最长的子序列。所以就用一个l数组存当子序列长度为len时最小的末尾元素。如果序列下一个值比l[len]大,说明上升子序列长度增加,那么l[len++]=a[i];如果是小,就想办法把它插入到了l数组中....
HDU 1950 说白了就是求lis:
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],l[maxn];
int len,n; int lis()
{
len=;
l[]=a[];
for (int i=;i<n;i++){
if(a[i]>l[len-])
l[len++]=a[i];
else
*lower_bound(l,l+len,a[i])=a[i];
}
return len;
} int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
for (int i=;i<n;i++)
cin>>a[i];
cout<<lis()<<endl;
}
return ;
}
这个是二分写法(感觉二分用的最广):
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],l[maxn];
int len,n; int bin_search(int key)
{
int left, right, mid;
left = , right = len;//应该是数据比较水,这样写是左闭右开区间查找,严格来说应该是right=len+1
while (left<right)
{
mid = (left + right) >> ;
if (l[mid] >= key)
right = mid;
else left = mid + ;
}
return left;
} int lis()
{
len = ;
l[] = a[];
for (int i = ; i <= n; i++) {
if (a[i] > l[len])
l[++len] = a[i];
else {
int pos = bin_search(a[i]);
l[pos] = a[i];
}
}
return len;
} int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
for (int i = ; i <= n; i++)
cin >> a[i];
cout << lis() << endl;
}
return ;
}
二分也可以这样写 (查找第一个大于或等于key的元素):
int bin_search(int key)
{
while(left<=right)
{
mid=(left+right)>>;
if(l[mid]>=key)
right=mid-;
else left=mid+;
}
return left;
}
还有一种是用stl里的set写,原理和上面一样,感觉用的好精妙:
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
const int MAXN = ;
set<int> st;
set<int>::iterator it;
int a[MAXN]; int main()
{
int T, n;
cin >> T;
while (T--)
{
st.clear();
cin >> n;
for (int i = ; i <= n; i++) {
cin >> a[i];
if (st.count(a[i])) continue;
st.insert(a[i]);
it = st.find(a[i]);
it++;
if (it != st.end())
st.erase(it);
}
cout << st.size() << endl;
}
return ;
}
当然,还有更精妙的:
#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = ;
int a[N],dp[N]; int main()
{
int T, n;
cin >> T;
while (T--)
{
cin >> n;
for (int i = ; i < n; i++) {
cin >> a[i]; dp[i] = INF;
}
for (int i = ; i < n; i++)
*lower_bound(dp, dp + n, a[i]) = a[i];
cout << lower_bound(dp, dp + n, INF) - dp << endl;
}
return ;
}
hdu 1950 最长上升子序列(lis) nlogn算法【dp】的更多相关文章
- AT2827 最长上升子序列LIS(nlogn的DP优化)
题意翻译 给定一长度为n的数列,请在不改变原数列顺序的前提下,从中随机的取出一定数量的整数,并使这些整数构成单调上升序列. 输出这类单调上升序列的最大长度. 数据范围:1<=n<=10 ...
- hdu 5773 最长递增子序列 (nlogn)+贪心
The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 1950 最长上升子序列
//Accepted 3540 KB 62 ms //dp 最长上升子序列 #include <cstdio> #include <cstring> #include < ...
- 最长上升子序列 LIS nlogn
给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的n个整数组成的序列. Output 最长上升子序 ...
- (转载)最长递增子序列 O(NlogN)算法
原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...
- 最长递增子序列 O(NlogN)算法
转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...
- 最长上升子序列O(nlogn)算法详解
最长上升子序列 时间限制: 10 Sec 内存限制:128 MB 题目描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.我们想知道此时最长上升子 ...
- nlogn 求最长上升子序列 LIS
最近在做单调队列,发现了最长上升子序列O(nlogn)的求法也有利用单调队列的思想. 最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]& ...
- 最长上升子序列 LIS(Longest Increasing Subsequence)
引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...
随机推荐
- LeetCode409Longest Palindrome最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...
- C#解析字符串公式
/// <summary> /// 中缀表达式到逆波兰表达式的转换及求值 /// </summary> public class RpnExpression { #region ...
- Sublime setting 个性修改
{ // 编辑器字体大小 "font_size": 10, // 设置行间距 "line_padding_bottom":5, "line_paddi ...
- 关于JavaScript的43道题①
最近在github上大火的43到js代码题,有很多人搬运.原链接https://github.com/lydiahallie/javascript-questions 1.下面代码的输出是什么? fu ...
- IO流2 --- File类的常用方法1 --- 技术搬运工(尚硅谷)
File类的获取功能 @Test public void test2(){ File file1 = new File("hello.txt"); File file2 = new ...
- 计蒜客 Red Black Tree(树形DP)
You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...
- 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...
- 【python之路14】发送邮件实例
1.发邮件的代码 from email.mime.text import MIMEText from email.utils import formataddr import smtplib msg ...
- Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...
- C#中时间差的计算
/// <summary> /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name=&q ...