二分优化lis和STL函数

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_bound和lower_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的好处;
这个题我们需要求出最长单调不升子序列和一个最长单调上升子序列;
这个题两种写法,学了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函数的更多相关文章
- 分治算法(二分查找)、STL函数库的应用第五弹——二分函数
分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...
- 二分优化的lis
/*此题为一个女大佬教我的,%%%%%%%%%%%%*/ 题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为 ...
- POJ 3903:Stock Exchange(裸LIS + 二分优化)
http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- 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 ...
- (LIS)最长上升序列(DP+二分优化)
求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...
- STL函数库的应用第四弹——全排列(+浅谈骗分策略)
因为基础算法快学完了,图论又太难(我太蒻了),想慢慢学. 所以暂时不写关于算法的博客了,但又因为更新博客的需要,会多写写关于STL的博客. (毕竟STL函数库还是很香的(手动滑稽)) 请出今天主角:S ...
- 51Nod 1090 3个数和为0 set 二分优化
给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到 ...
- hdu5256 二分求LIS+思维
解题的思路很巧,为了让每个数之间都留出对应的上升空间,使a[i]=a[i]-i,然后再求LIS 另外二分求LIS是比较快的 #include<bits/stdc++.h> #define ...
随机推荐
- STP(Spanning Tree Protocol)
STP生成树协议 问题 为了提高网络的可用性,需要进行冗余和备份.但是冗余路径会产生环路 环路会导致以下问题 广播风暴:由于交换机会对广播.多播.和未知目标MAC的单播包进行泛洪,在存在环路的情况 ...
- python学习笔记3-列表
# 1.列表长度可变,内容可修改 a = [0,1,2,3] a[0] = 'a0' a # ['a0', 1, 2, 3] # 2.添加元素 # 2.1列表末尾添加元素 a.append(4) a ...
- 【LeetCode每天一题】Plus One(加一)
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The d ...
- windows下gitbash中使用zip命令
参考: https://ranxing.wordpress.com/2016/12/13/add-zip-into-git-bash-on-windows/
- 201904<<快速阅读术>>
在看过了几本数之后,发现原来培养读书的习惯好像也不太难,“将读书融入生活,框定读书时间” 生活中,我确实也是这样执行了.利用每天上下班的时间听书,有些觉得可以读快的书籍用了1.5倍速度在听,难懂的部分 ...
- 18.12.02-C语言练习:韩信点兵
C语言练习:韩信点兵 题目说明:本题是中国经典问题,有多种解法,从数论课程角度看,是一个不定方程组,而且答案不唯一. 但这里采用程序解法,使用的是暴力破解.枚举可能的解,然后根据条件判断,满足所有条件 ...
- Hashtable几种常用的遍历方法
Hashtable 在System.Collection是命名空间李Hashtable是程序员经常用到的类,它以快速检索著称,是研发人员开发当中不可缺少的利器. Hashtable表示键/值对的集合, ...
- Two (DP)
题意:求两串数字有多少个相同的子串,子串不要求连续. 思路:直接DP,dp[i][j] 代表A串长度为i.B串为j时满足条件的子串个数.转移dp[i][j] = dp[i -1][j] + dp[i] ...
- 微信不支持App下载的解决方案 微信跳转打开外部浏览器下载(苹果跳转商店下载)
在微信中,打开app下载链接,或者使用微信扫一扫app下载二维码,都是无法下载app的. 因为腾讯为了自身利益,屏蔽了其他app直接在微信中下载.下面给分享下,找到的2种有效的解决方案. 方案:点击链 ...
- class多态
多态代码实现: class Animal(object): def __init__(self, name): # Constructor of the class self.name = name ...