C. Chat Ban (二分

添加链接描述
尤其注意全局变量和局部变量的重复定义

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k,x;
ll sum(ll mid){
    ll ans=0;
    if(mid<=k)ans=(1+mid)*mid/2;
    if(mid>k){
        ans=(k-1+k-(mid-k))*(mid-k)/2;
        // cout<<ans<<"///";
        ans+=(1+k)*k/2;
    }
    return ans;
}
bool solve(ll mid){
    // cout<<mid<<"---";
    // cout<<k<<endl;
    ll ans=0;
    ans=sum(mid);
    //  cout<<ans<<endl;
    if(ans>x)return 0;
    else return 1;
}
int main(){
    int T;
    cin>>T;
    while(T--){
        
        cin>>k>>x;
        ll l=1,r=2*k-1;
       // cout<<l<<" "<<r<<" "<<endl;
        while(l<r){
            ll mid=l+r+1>>1;
            if(solve(mid)) l=mid;
            else r=mid-1;
            // cout<<l<<"***"<<r<<endl;
        }
        //cout<<"--"<<endl;
        
        if(sum(l)<x&&l<2*k-1)cout<<l+1<<endl;
        else cout<<l<<endl;
    }
   

    return 0;
}
上一篇:12-14日记


下一篇:uni-app 126删除指定消息记录