Frodo and pillows CodeForces - 760B 二分 注意l和r的选择

 以后写l和r的初始值的时候,在不影响合理性的前提下,尽量写大一点

比如下面这个代码,如果r不加以或者l==0就不行

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include<iomanip>
#include <queue>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1e5+5;
typedef long long ll;
ll n,m,k; 
bool check(ll x){
    ll res = 0;
    if( x-(k-1)<1 ){
        res+=(x+1)*x/2;
        res+=( k-x );
    }
    else{
        res+=(2*x-k+1)*k/2;
    }
    if( n>k+x-1 ){
        res+= (x+1)*x/2;
        res+=( n-( k+x-1 ) );
    }
    else{
        res+=( x*2-n+k )*(n-k+1)/2;
    }
    return res-x <= m;
} 
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m>>k;
    ll l = 0,r = m+1;//这里r必须加一 
    while(l<r){ 
        ll mid = (l+r)/2;
        if( check(mid) ) l = mid + 1; //mid太小了 
        else r = mid;
    } 
    cout<<l-1 <<endl;
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/SunChuangYu/p/12635067.html