最长递增子序列 nlogn
2017-04-10 00:09
225 查看
#include<iostream>
using namespace
std;
//2 1 5 3 6 4 8 9 7
//上课讲了这个,回去之后就用代码实现以下
// 这个算法的目标是
找到一个最长的递增子序列
//O(NlogN)的复杂度
//思路是遇到小的就留下,更新的路程中
插入到这个序列当中
//一开始看那个麻瓜blog理解错了
//然后看这个理解了动态规划里的算法,没看懂意思
//http://www.360doc.com/content/16/0427/06/478627_554122487.shtml
//再回到麻瓜blog之后
了解意思了
准备coding一下
//二分查最大的比它小的位置。
int binary_find(int tar,int l,int r,int*
a){
if(l==r)
return l;
int mid=(l+r)/2;
//这个符号该怎么定义呢
if(tar<a[mid])
return
binary_find(tar,l,mid,a);
else
return
binary_find(tar, mid+1, r, a);
}
int* getlist(int *a){
int* temp=new
int[9];
int len=0;
//temp[0]=0;
for (int i=1;i<9;i++){
if(a[i]<a[temp[len]])
temp[binary_find(a[i],
0, len, a)]=i;
else{
len++;
temp[len]=i;
4000
}
}
for(int i=0;i<len+1;i++){
temp[i]=a[temp[i]];
}
//len=5;
//cout
int *out=new
int[len];
//len--;
out[len]=a[temp[len]];
cout<<"thresh"<<out[len]<<endl;
for(int i=temp[len];i>-1;i--){
cout<<"len:"<<len<<endl;
//cout<<out[len];
if(a[i]<out[len]){
out[len-1]=a[i];
len--;
cout<<out[len]<<endl;
}
}
//2 1 5 3 6 4 8 9 7
return out;
}
int main(){
int a[9]={2,1,5,3,6,4,8,9,7};
int *b=getlist(a);
for(int i=0;i<5;i++)
cout<<b[i]<<" ";
cout<<endl;
}
using namespace
std;
//2 1 5 3 6 4 8 9 7
//上课讲了这个,回去之后就用代码实现以下
// 这个算法的目标是
找到一个最长的递增子序列
//O(NlogN)的复杂度
//思路是遇到小的就留下,更新的路程中
插入到这个序列当中
//一开始看那个麻瓜blog理解错了
//然后看这个理解了动态规划里的算法,没看懂意思
//http://www.360doc.com/content/16/0427/06/478627_554122487.shtml
//再回到麻瓜blog之后
了解意思了
准备coding一下
//二分查最大的比它小的位置。
int binary_find(int tar,int l,int r,int*
a){
if(l==r)
return l;
int mid=(l+r)/2;
//这个符号该怎么定义呢
if(tar<a[mid])
return
binary_find(tar,l,mid,a);
else
return
binary_find(tar, mid+1, r, a);
}
int* getlist(int *a){
int* temp=new
int[9];
int len=0;
//temp[0]=0;
for (int i=1;i<9;i++){
if(a[i]<a[temp[len]])
temp[binary_find(a[i],
0, len, a)]=i;
else{
len++;
temp[len]=i;
4000
}
}
for(int i=0;i<len+1;i++){
temp[i]=a[temp[i]];
}
//len=5;
//cout
int *out=new
int[len];
//len--;
out[len]=a[temp[len]];
cout<<"thresh"<<out[len]<<endl;
for(int i=temp[len];i>-1;i--){
cout<<"len:"<<len<<endl;
//cout<<out[len];
if(a[i]<out[len]){
out[len-1]=a[i];
len--;
cout<<out[len]<<endl;
}
}
//2 1 5 3 6 4 8 9 7
return out;
}
int main(){
int a[9]={2,1,5,3,6,4,8,9,7};
int *b=getlist(a);
for(int i=0;i<5;i++)
cout<<b[i]<<" ";
cout<<endl;
}
相关文章推荐
- 最长递增子序列 O(n^2) 与 O(nlogn)
- 最长递增子序列,时间复杂度(O(nlogn))
- 最长递增子序列,时间复杂度(O(nlogn))
- 【难】求数组中最长递增子序列,时间复杂度O(nlogn)
- (转载)最长递增子序列 O(NlogN)算法
- 最长递增子序列(O(nlogn))
- 【转】最长递增子序列 O(NlogN)算法
- (转载)最长递增子序列 O(NlogN)算法
- 最长单调递增子序列 o(n^2),o(nlogn)
- 最长递增子序列-O(NlogN)
- 找出n个数组成的最长单调递增子序列( 动态规划O(nlogn) )
- 最长递增子序列 O(NlogN)算法
- 51nod 1134 最长递增子序列(dp) 时间复杂度为O(nlogn)
- LeetCode 最长递增子序列的O(nlogn)详解
- 模板[最长递增子序列] O(nlogn)
- 最长递增子序列(LIS)的O(NlogN)打印算法
- 51 Nod 1134 最长递增子序列 LIS NlogN 写法
- 最长递增子序列 O(NlogN)算法
- POJ 3903 【最长递增子序列 NLOGN】
- 最长递增子序列 O(nlogn)