二分

二分算法模板

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;
}

参考博客:https://blog.csdn.net/wbin233/article/details/77570070

【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】的更多相关文章

  1. 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 ...

  2. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...

  3. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  4. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  5. 二分查找(lower_bound和upper_bound)

    转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...

  6. 二分查找法(binary_search,lower_bound,upper_bound,equal_range)

    binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...

  7. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

  8. 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 ...

  9. C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique

    std::sort      对vector成员进行排序; std::sort(v.begin(),v.end(),compare);   std::lower_bound 在排序的vector中进行 ...

随机推荐

  1. 小白也能看懂的 Laravel 核心概念讲解

    自动依赖注入 什么是依赖注入,用大白话将通过类型提示的方式向函数传递参数. 实例 1 首先,定义一个类: /routes/web.php class Bar {} 假如我们在其他地方要使用到 Bar  ...

  2. EditPlus VC2010 and 2008 C/C++配置

    源自:http://blog.csdn.net/weiling_shen/archive/2010/03/26/5421017.aspx 对于2010跟2008差不多,只需相应的修改一下路径即可:如2 ...

  3. CodeForces 696A:Lorenzo Von Matterhorn(map的用法)

    http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...

  4. 致远A8任意文件写入漏洞_getshell_exp

    近期爆出致远 OA 系统的一些版本存在任意文件写入漏洞,远程攻击者在无需登录的情况下可通过向 URL /seeyon/htmlofficeservlet POST 精心构造的数据即可向目标服务器写入任 ...

  5. ajax 的登录认证

    在models中 先创建一个表 from django.db import models # Create your models here. class UserInfo(models.Model) ...

  6. 关于在使用scrapy-redis分布式踩过的那些坑:

    自己的案列:win7上安装ubuntu (win7作为slaver,ubuntu作为master ) 修改配置文件redis.conf 1)打开配置文件把下面对应的注释掉 # bind 127.0.0 ...

  7. 二、JavaScript的语法

    目录: 1.变量:存储数据的容器 2.数据类型 3.string数据类型 4.number数据类型 5.boolean数据类型 6.数据类型的隐式转换 6.数据类型转换函数 7.特殊类型 8.算术运算 ...

  8. Connecting to the Network

    This lesson shows you how to implement a simple application that connects to the network. It explain ...

  9. 微信小程序开发--API界面交互

    一.wx:showActionSheet(上拉菜单) 属性 类型 默认值 必填 说明 itemList Array.<string>   是 按钮的文字数组,数组长度最大为 6 itemC ...

  10. Maven(一)Maven 的概念和安装

    Maven 的概念和安装 Maven 是什么 首先 Maven 肯定是一个造福人类的好东西,它可以省去我们构建项目中引入 jar 包时的麻烦,还有利于项目的模块化开发等等等好处.在如今项目中大体都是使 ...