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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛阳八中我最棒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值