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. 与eslint有关的规范

    https://cloud.tencent.com/developer/section/1135682 腾讯云的规范还是不错的

  2. Unicode编码范围

  3. Spring Boot 对请求的映射

    在SpringBoot中对请求资源的映射有三种方式: 1.通过编写一个Controller请求,获得客户端发送过来的请求就转发出去 //通过这种方式可以来映射页面请求路径 @PostMapping(& ...

  4. java8新特性学习笔记链接

    https://blog.csdn.net/yitian_66/article/details/81010434

  5. 初尝Spring Cloud Config

    1,下载源码 地址https://spring.io/guides/gs/centralized-configuration/ 2,导入工程 解压后分别把Server端与Client端导入到两个Ecl ...

  6. 在C#中,Newtonsoft.Json + dynamic动态解析jsonString,jsonString转实体

    记录一下 引用 using Newtonsoft.Json; using Newtonsoft.Json.Linq; var jsonString = "{\"ApiResourc ...

  7. cocos2d-x C++ 获取网络图片缓存并展示

    #ifndef __HttpGetImg__ #define __HttpGetImg__ #include "cocos2d.h" #include "HttpRequ ...

  8. Spring增强代理模式

    1. 依赖注入;(掌握) 2. XML自动注入;(掌握) 3. 全注解配置;(掌握) 4. 代理模式;(掌握,难点) 依赖注入 构造参数注入 constructor-arg:构造器注入: index: ...

  9. 1#认识Java

    Java是一种面对对象的编程语言. Java共分为三个体系:JavaEE.JavaSE.JavaMS Java SE 1: Java Platform Standard Edition,Java平台标 ...

  10. feifeicms后台任意文件读取

    前台大略看了下,本身内容比较简单,经过“洗礼”后以及没什么问题了,基本上输入都过滤了. 这次审计找到了一个后台的任意文件读取,可以读取数据库配置文件. 在DataAction.class.php文件中 ...