二分的研究

二分查找的标准模板
    int l = 0,r = len-1;
    while (l <= r){
        int m = (l+r)>>1;
        if (judge(m))l = m+1;
        else         r = m-1;
    }
    cout << l-1 << endl;

此模板,主要判断的是找到最后一个满足条件的值
附上大神的链接

二分查找的注意点: 点击打开链接
二分查找的变种   : 点击打开链接
二分习题集          : 点击打开链接

经典例题: 符合条件的最小值

                                             Monthly Expense
Description

Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

Input

Line 1: Two space-separated integers:   N  and   M  
Lines 2.. N+1: Line   i+1 contains the number of dollars Farmer John spends on the   ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

相当于是寻找第一个大于等于key的值
// 目标找到第一个大于等于所分组的条件
#include <iostream>
using namespace std;

const int MAXN = (int)1e5+7;
int a[MAXN];
int N,P;

//x 偏大则返回 true (x >= result)
bool Fit(int x){
    int sum = 0,team = 1;
    for (int i = 0;i < N;i ++){
        if (sum + a[i] > x){
            team ++;
            i --;
            sum = 0;
        }else {
            sum += a[i];
        }
        if (team > P) {
            return false;
        } //分组过多说明x偏小
    }
    return true;                    //返回x >= result的情况
}

int FindFirMax(){
    int l = 0,r = 1e9;
    while (l <= r){
        int m = l + ((r-l)>>1);
        if (Fit(m))r = m-1;
        else       l = m+1;
    }
    return l;
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> N >> P;

    for (int i = 0;i < N;i ++)cin >> a[i];

    int ans = FindFirMax();
    cout << ans << endl;
}

前缀和

A

NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为n 的数列,他又根据心情写下了一个数 m他想知道这个数列中有多少个区间里的第 k 大的数不小于 m,当然首先这个区间必须至少要有 kkk 个数啦。

Solution 用前缀和保存前i个区间有多少不小于m的数,对于左端点for过去,然后二分又端点,如果二分到某个又端点恰好满足(l,r)之中第k大数不小于m,那么又端点右边的区间都符合。本题也可以用尺取法来做。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MAXN = (ll)2e5+7;

ll Sum[MAXN];
ll N,M,K;

bool Judge(ll l,ll r){
    if (Sum[r] - Sum[l-1] >= K)return true;
    return false;
}

int main()
{
    ios::sync_with_stdio(false);

    ll T;
    cin >> T;
    while (T --){
        cin >> N >> M >> K;
        memset(Sum,0,sizeof(Sum));

        ll tem;
        for (ll i = 1;i <= N;i ++){
            cin >> tem;
            Sum[i] += Sum[i-1] + (tem >= M ? 1 : 0);
        }

        ll ans = 0;
        for (ll i = 1;i+K-1 <= N;i ++){
            ll l = i,r = N;
            while (l <= r){
                //jia
                //cout << "l = " << l << ",r = " << r << endl;
                ll m = (l+r)>>1;
                if (Judge(i,m)) r = m-1;
                else            l = m+1;
            }
            //jia
            //cout << "i = " << i << ",ans + " << N-l+1 << endl;
            ans += N-l+1;
        }
        cout << ans << endl;
    }
}
/*
2
7 4 2
4 2 7 7 6 5 1

7 2 1
1 1 1 1 1 1 1
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值