最长上升子序列 和 最长不下降子序列
2015-06-01 14:31
281 查看
最长上升子序列:是严格上升的,a<b<c<d这样的,即为严格单调递增,用lower_bound
lower_bound:返回序列中大于等于key值的第一个数
比如说序列 1 2 3 4 5,key值为3,使用lower_bound返回第三个数
最长不下降子序列:不是严格上升,可以存在多个数相等的情况,用upper_bound
upper_bound:返回序列中严格大于key值的第一个数
比如说序列1 2 3 4 5,key值为3,使用upper_bound返回第四个数
Hdu 1950 求最长上升子序列
View Code
lower_bound:返回序列中大于等于key值的第一个数
比如说序列 1 2 3 4 5,key值为3,使用lower_bound返回第三个数
最长不下降子序列:不是严格上升,可以存在多个数相等的情况,用upper_bound
upper_bound:返回序列中严格大于key值的第一个数
比如说序列1 2 3 4 5,key值为3,使用upper_bound返回第四个数
Hdu 1950 求最长上升子序列
#include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #include<set> #include<queue> #include<algorithm> using namespace std; typedef long long LL; const int INF = (1<<30)-1; const int mod=1000000007; const int maxn=1000005; int a[maxn],f[maxn]; int main(){ int T; scanf("%d",&T); int kase=0; while(T--){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]=a[i]-i; } int len=1; f[1]=a[1]; for(int i=2;i<=n;i++){ if(a[i]>=f[len]) f[++len]=a[i];//这里也和求最长上升子序列不同,是大于等于 else{ int pos=upper_bound(f+1,f+len+1,a[i])-f;//upper_bound返回的是序列中严格大于key值的第一个数 f[pos]=a[i]; } } printf("Case #%d:\n",++kase); printf("%d\n",n-len); } return 0; }
View Code
相关文章推荐
- C++程序设计 - Week 1 从C走进C++
- STM32 串口配置 注意事项
- 解读各种PS图层混合模式的工作原理
- 兼容所有浏览器的js关闭当前网页代码
- 使用PowerDesigner15设计表模型的时候,如何给各个字段添加注释。
- 什么是SAP重复性生产
- zookeeper 学习记录3 (session,watcher,persit机制)
- zookeeper 学习记录2
- windows 版Tomcat 7.0的配置
- AngularJs中,如何在render完成之后,执行Js脚本
- TCP慢启动与拥塞控制笔记
- UML基础(二)——UML中的类(接口)以及类关系的表示方法
- 给 程序员 的设计学习指南
- 合成模式代码示例
- android应用开发环境配置
- 第十三周项目一动物这样叫(1)
- 初窥Python(一)——使用pymongo连接MongoDB
- 222_尚学堂_高淇_java300集最全视频教程_JVM核心机制_深入类加载器_自定加密解密类加载器
- VDI中配置Chrome的AppData重定向,无法保存收藏夹
- 公安部:微博、微信和百度贴吧上线网警巡查执法账号