LIS:最长上升子序列;

这个题我们很显然会想到使用dp,

状态设计:dp[i]代表以a[i]结尾的LIS的长度 
状态转移:dp[i]=max(dp[i], dp[j]+1) (0<=j< i, a[j]< a[i]) 
边界处理:dp[i]=1 (0<=j< n) 
时间复杂度:O(N^2) 

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
using namespace std;
const int MAXN=;
int n,a[MAXN],dp[MAXN];
int LIS()
{
int ans=;
for(int i=;i<=n;i++)
{
dp[i]=;
for(int j=;j<i;j++)
if(a[i]>a[j]) dp[i]=max(dp[i],dp[j]+);
ans=max(ans,dp[i]);
}
return ans;
}
int main()
{
n=read();
for(int i=; i<=n; i++)
cin>>a[i];
int ans=LIS();
cout<<ans<<endl;
return ;
}

但是n^2的做法显然会超时,所以介绍一种二分优化的做法;

用二分+贪心的思想可以将时间复杂度优化至(nlogn);

a[i]表示第i个原数据。 
dp[i]表示表示长度为i+1的LIS结尾元素的最小值。 
利用贪心的思想,对于一个上升子序列,当前添加的最后一个元素越小,越有利于添加新的元素,这样LIS长度更长。 
因此,我们只需要维护dp数组,其表示的就是长度为i+1的LIS结尾元素的最小值,保证每一位都是最小值,

这样子dp数组的长度就是LIS的长度。

这样每次查找就用到了我们的stl函数撒;

介绍一下upper_boundlower_bound;

(刚知道这个东西)

lower_bound( )和upper_bound( )是利用二分查找的方法在一个有序的数组中进行查找的。

当数组是从小到大时,

lower_bound( begin,end,num):表示从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,找到数字在数组中的下标。

upper_bound( begin,end,num):表示从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,找到数字在数组中的下标。

当数组是从大到小时,我们需要重载lower_bound()和upper_bound();

struct cmp{bool operator()(int a,int b){return a>b;}};

lower_bound( begin,end,num,cmp() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,cmp() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

所以我们就可以使用stl函数寻找lis啦;

针对上面那个题:

#include<bits/stdc++.h>
using namespace std;
#define N 500001
inline int read()
{
int x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
int n,a[N],l[N];
struct cmp{bool operator()(int a,int b){return a>b;}};
int main()
{
n=read();
for(int i=;i<=n;i++) a[i]=read();
int con=,cont=;
l[]=a[];
for(int i=;i<=n;i++)
{
if(l[cont]<a[i]) l[++cont]=a[i];
else l[upper_bound(l+,l+cont+,a[i])-l]=a[i];
}
cout<<cont<<endl;
return ;
}

所以我们想一下有没有什么dp的题可以用stl写呢?

嗯...导弹拦截,这个题可以完美的体现stl的好处;

luogu

这个题我们需要求出最长单调不升子序列和一个最长单调上升子序列;

这个题两种写法,学了stl后又写了一个,明显stl代码短很多;

因为洛谷输入和本校oj不太一样,酌情修改代码...

#include<bits/stdc++.h>
using namespace std;
int a[],f[],l[],n;
struct cmp{bool operator()(int a,int b){return a>b;}};
int main()
{
// int n=1;
// while(cin>>a[n]) n++;
// n--;
cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
int con=,cont=;
l[]=f[]=a[];
for(int i=;i<=n;i++)
{
if(l[cont]>=a[i])l[++cont]=a[i];
else l[upper_bound(l+,l+cont+,a[i],cmp())-l]=a[i];
if(f[con]<a[i])f[++con]=a[i];
else f[lower_bound(f+,f+con+,a[i])-f]=a[i];
}
cout<<cont<<endl<<con;
return ;
}
/*
#include<iostream>
using namespace std;
int n;
int h[1001],ht[1001],best[1001];
int ans=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>h[i];
best[0]=0x7fffffff;
for(int i=1;i<=n;i++)
for(int j=ans;j>=0;j--)
if(best[j]>=h[i])
{
best[j+1]=h[i];
ans=max(ans,j+1);
break;
}
cout<<ans<<endl;
ans=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=ans;j++)
{
if(ht[j]>=h[i])
{
ht[j]=h[i];
break;
}
}
if(ht[ans]<h[i])ht[++ans]=h[i];
}
cout<<ans;
return 0;
}*/

看了好多手写二分的都快一二百行了,可是我还不会啊...

所以懒,写了stl函数,才知道代码的精短,核心不到十行;

总结:寻找最长上升(使用lower_bound)和最长不下降时(使用upper_bound),无需重载;

寻找最长下降(使用lower_bound)和最长不上升时(使用upper_bound),需重载;

#include<bits/stdc++.h>
using namespace std;
const int N=7e5+;
template<typename T>inline void read(T &x)
{
x=;T f=,ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
x*=f;
}
long long n,f[N],s[N],t[N],l[N],a[N],ans1,ans2,ans3,ans4;
struct cmp{bool operator()(int a,int b){return a>b;}};
int main()
{
read(n);
for(int i=;i<=n;i++)
read(a[i]);
l[]=f[]=s[]=t[]=a[];
ans1=ans2=ans3=ans4=;
for(int i=;i<=n;i++)
{
if(f[ans1]<a[i]) f[++ans1]=a[i];
else f[lower_bound(f+,f+ans1+,a[i])-f]=a[i];
if(s[ans2]>a[i]) s[++ans2]=a[i];
else s[lower_bound(s+,s+ans2+,a[i],cmp())-s]=a[i];
if(t[ans3]>=a[i]) t[++ans3]=a[i];
else t[upper_bound(t+,t+ans3+,a[i],cmp())-t]=a[i];
if(l[ans4]<=a[i]) l[++ans4]=a[i];
else l[upper_bound(l+,l+ans4+,a[i])-l]=a[i];
}
printf("%lld\n%lld\n%lld\n%lld\n",ans1,ans2,ans3,ans4);
return ;
}

二分优化lis和STL函数的更多相关文章

  1. 分治算法(二分查找)、STL函数库的应用第五弹——二分函数

    分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...

  2. 二分优化的lis

    /*此题为一个女大佬教我的,%%%%%%%%%%%%*/ 题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为 ...

  3. POJ 3903:Stock Exchange(裸LIS + 二分优化)

    http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  5. HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Problem Des ...

  6. (LIS)最长上升序列(DP+二分优化)

    求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...

  7. STL函数库的应用第四弹——全排列(+浅谈骗分策略)

    因为基础算法快学完了,图论又太难(我太蒻了),想慢慢学. 所以暂时不写关于算法的博客了,但又因为更新博客的需要,会多写写关于STL的博客. (毕竟STL函数库还是很香的(手动滑稽)) 请出今天主角:S ...

  8. 51Nod 1090 3个数和为0 set 二分优化

    给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到 ...

  9. hdu5256 二分求LIS+思维

    解题的思路很巧,为了让每个数之间都留出对应的上升空间,使a[i]=a[i]-i,然后再求LIS 另外二分求LIS是比较快的 #include<bits/stdc++.h> #define ...

随机推荐

  1. [04-05]box框模型(Box Model)定义了元素框处理元素内容、内边距、边框和外边距的方式

    实际占有的宽 = width + 2padding(内边距) + 2border(边框) + 2margin(外边距) 实际占有的高 = height + 2padding + 2border + 2 ...

  2. Jmeter设置默认中文页面

    方法一(从网上看到的) 启动Jmeter找到 options >choose language >chinese(简体繁体自己选). 这样设置后界面就变成了中文,但是当我们下次打开时又恢复 ...

  3. VS2017 SVN插件-AnkhSVN

    AnkhSVN 该插件可以直接在vs017扩展和工具里安装,安装完成即可使用 默认VS自带的源码管理工具是GIT 如果已经使用需要手动切换到SVN:工具==>选项菜单中设置 SVN使用方法: 1 ...

  4. python GUI图形化编程-----wxpython

    一.python gui(图形化)模块介绍: Tkinter :是python最简单的图形化模块,总共只有14种组建 Pyqt     :是python最复杂也是使用最广泛的图形化 Wx       ...

  5. 使用LVM进行分区扩展的记录

    场景:在磁盘分区空间不够的情况下,要扩展分区空间 因为使用的是虚拟机,所以可以对原有的硬盘上进行扩展,而不需要新增一个硬盘 1.扩展磁盘并使用fdisk工具进行分区 虚拟机关机后对磁盘进行扩展,扩展到 ...

  6. MongoDB系列----uupdate和数组

    db.collection.update( criteria, objNew, upsert, multi ) criteria : update的查询条件,类似sql update查询内where后 ...

  7. Cocos Creator 动态设置Canvas的宽度与高度,更改适配

    let c = this.node.getComponent(cc.Canvas);c.fitHeight = true;c.fitWidth = false; let h = 960 * cc.wi ...

  8. 又见thrift异常之TApplicationException: Internal error processing..

    客户端调用获取商户提现产品手续费的接口,出现异常org.apache.thrift.TApplicationException: Internal error processing getMercha ...

  9. 在eclipse中的交叉编译

    1.硬件是Arm 9的板子,运行的系统是Ubuntu 12.05 2.电脑虚拟机上安装的系统是Ubuntu 10.04,程序的开发都是在虚拟机上完成,开发IDE是eclipse 3.eclipse 默 ...

  10. Qt QLineEdit 漂亮的搜索框 && 密码模式 && 格式化输入 && 提示文字 && 选择内容并移动 && 清除全部输入

    先上一个漂亮的搜索框效果图, 输入搜索文本效果, 点击搜索图标效果: //实现代码 void MainWindow::iniLineEdit() { ui->lineEdit->setPl ...