zoukankan      html  css  js  c++  java
  • stl 的 二分函数

    在stl里面有二分函数

    lower_bound  和 upper_bound;

    第一参数 为起始地址 第二个为结束地址 注意 它是左闭右开的(即不包括结束地址对应的那个值)

    第三个参数为要找的值; 两个二分函数都是要在由小到大的序列里。

    作用 : lower_bound 返回第一个大于等于第三个参数的的地址 upper_bound返回第一个大于第三个参数的地址;

    然后我们开始介绍下第四个参数

    比较函数 (我也不太清楚是什么)

    只要你在第四个参数上面 打上 greater<type>()(他的头文件 #include<functional>)

    lower_bound 就会 变成 返回数组中第一个小于或等于被查数的地址

    upper_bound 就会 变成 返回数组中第一个小于被查数的地址

    so interesting

    附上求最长下降子序列的代码

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<ctime>
    #include<functional>
    #include<map>
    #include<cmath>
    #include<deque>
    #define M 5100
    #define min(x , y) (x) < (y) ? (x) : (y)
    #define max(x , y) (x) > (y) ? (x) : (y)
    #define inf 0x3f3f3f3f
    #define ll long long
    using namespace std;
    int n , f[M] , ans = -inf , a[M];
    int main(){
        freopen("c1.in" , "r" , stdin);
        scanf("%d" , &n);
        for (int i = 1 ; i <= n ; ++i){
            scanf("%d" , a + i);
        } 
        f[0] = inf;
        for (int i = 1 ; i <= n ; ++i){
            int now = lower_bound(f + 1 , f + n + 1 , a[i] , greater<int>()) - f;
            f[now] = max(a[i] , f[now]);
        }
        for (int i = 1 ; f[i] ; ++i) ans = i;
        printf ("%d" , ans);
        return 0;
    }
  • 相关阅读:
    函数的参数设置
    定义函数
    使用dict和set
    (转)set集合的应用
    循环与range
    if语句
    How to use git hub
    Install pyodbc in OpenSUSE
    Ubuntu编译安装PHP7
    Ubuntu为已经安装的PHP7单独编译mysqli
  • 原文地址:https://www.cnblogs.com/LYFmobai/p/10053825.html
Copyright © 2011-2022 走看看