【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】
二分
二分算法模板
int bsearch(int *a, int x, int y, int v)
{
int mid;
while(x < y)
{
mid = (x + y) / 2;
if(a[mid] == v)
return mid;
if(a[mid] > v)
y = mid;
else
x = mid + 1;
}
return -1;
}
注意此模板只适用于查找a中是否存在v,存在的话则返回其中一个符合条件的位置,并不一定只有那一个位置,这个视情况而定。
lower_bound
lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址)
upper_bound
upper_bound()与lower_bound()的主要区别在于前者返回第一个大于目标值的位置(地址)
int lowerBound(int x){
int l=1,r=n;
while(l<=r){
int mid=(l+r)>>1;
if (x>g[mid]) l=mid+1;
else r=mid-1;
}
return l;
}
int upperBound(int x){
int l=1,r=n;
while(l<=r){
int mid=(l+r)>>1;
if (x>=g[mid]) l=mid+1;
else r=mid-1;
}
return l;
}
最长上升子序列LIS
这里提供一组代码,它用于返回数组b中最长的上升子序列的长度
int cal(int *b)
{
vector<int> s;
for(int i = 0; i < n ;i++)
{
if(s.empty()) s.push_back(b[i]);
else
{
vector<int>::iterator it = upper_bound(s.begin(), s.end(), b[i]);
if(it == s.end()) s.push_back(b[i]);
else
*it = b[i];
}
}
return s.size();
}
对应题目:HDU-5532、HDU-6197
**再介绍一个用dp求最长子序列长度的算法,对应紫书P274,状态方程为dp[i]=max ( dp[ i ], dp[ j ]+1 ) ( 0<=j< i, a[ j ] < a[ i ] ) **
#include <bits/stdc++.h>
using namespace std;
const int MAXX=100000+5;
const int INF=INT_MAX;
int a[MAXX],dp[MAXX]; // a数组为数据,dp[i]表示以a[i]结尾的最长递增子序列长度
int main()
{
int n;
while(cin>>n)
{
for(int i=0; i<n; i++)
{
cin>>a[i];
dp[i]=1; // 初始化为1,长度最短为自身
}
int ans=1;
for(int i=1; i<n; i++)
{
for(int j=0; j<i; j++)
{
if(a[i]>a[j])
{
dp[i]=max(dp[i],dp[j]+1); // 状态转移
}
}
ans=max(ans,dp[i]); // 比较每一个dp[i],最大值为答案
}
cout<<ans<<endl;
}
return 0;
}
【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- 二分查找(lower_bound和upper_bound)
转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...
- 二分查找法(binary_search,lower_bound,upper_bound,equal_range)
binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
- Long Jumps(二分查找lower_bound()函数的运用)
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long ju ...
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
std::sort 对vector成员进行排序; std::sort(v.begin(),v.end(),compare); std::lower_bound 在排序的vector中进行 ...
随机推荐
- 【mysql5.7】远程无法连接设置
版本5.7 系统:ubuntu16.04 配置文件位置(apt安装): 1.链接设置 注释掉在/etc/mysql/mysql.conf.d/mysqld.cnf里面的bind-address = 1 ...
- 两张图示轻松看懂 UML 类图
一个类如何表示 第一格为类名 第二格为类中字段属性 格式:权限 属性名:类型 [ = 默认值 ] 权限:private.public .protected.default,它们分别对应 -.+.#.~ ...
- Visual Studio Code配置
Visual Studio Code 从1.23.0开始VS Code就不再默认提供各语言版本, 而是改为使用插件的方式提供语言包. 在插件商店搜索Chinese (Simplified), 安装. ...
- CCPC2019江西省赛-Problem G.Traffic
题目描述: /*纯手打题面*/ Avin is observing the cars at a crossroads.He finds that there are n cars running in ...
- 透视BlueStore存储结构:如何根据文件名从裸盘提取文件内容
在FileStore下,用户文件经过切分对象块后最终存放在了单机文件系统(xfs .ext4等)中,我们可以较容易地找到这些对象块对应的文件,然后提取这些对象块文件后组装成用户文件.然而,BlueSt ...
- Go - Map 集合
目录 概述 声明 Map 生成 JSON 编辑和删除 推荐阅读 概述 Map 集合是无序的 key-value 数据结构. Map 集合中的 key / value 可以是任意类型,但所有的 key ...
- kafka源码分析(二)Metadata的数据结构与读取、更新策略
一.基本思路 异步发送的基本思路就是:send的时候,KafkaProducer把消息放到本地的消息队列RecordAccumulator,然后一个后台线程Sender不断循环,把消息发给Kafka集 ...
- 为什么我使用 Linux 开发
Linux 能用吗? 当我对 Linux 的印象似乎还停留在黑乎乎的命令行界面上的时候,我身边的一些朋友告诉我或者建议我使用 Linux 时,我会一脸惊讶的问他,那个怎么用(来开发或者日常使用)? L ...
- django基础知识之认识MVT MVC:
MVT Django是一款python的web开发框架 与MVC有所不同,属于MVT框架 m表示model,负责与数据库交互 v表示view,是核心,负责接收请求.获取数据.返回结果 t表示templ ...
- springboot+redis实现session共享
1.场景描述 因项目访问压力有点大,需要做负载均衡,但是登录使用的是公司统一提供的单点登录系统,需要做session共享,否则假如在A机器登录成功,在B机器上操作就会存在用户未登录情况. 2. 解决方 ...