硬核二分——cf985D
分两种情况进行讨论,要注意判条件时会有爆ll
#include<bits/stdc++.h> using namespace std; #define ll long long ll n,h; int judge1(ll mid){//mid*(mid+1)>=2*n if((double)(mid+1)>=2.0*n/mid) return 1; return 0; } int judge2(ll mid){//mid 是答案 ll tmp=(mid-h+1)/2; ll sum=(h+h+tmp-1)*tmp+(h-1)*h/2; if((mid-h+1)%2) sum+=(h+tmp); if(sum>=n)return 1; return 0; } int main(){ cin>>n>>h; if((double)(h+1)>=2.0*n/h){//h*(h+1)/2>=n ll l=1,r=h,ans,mid; while(l<=r){ mid=l+r>>1; if(judge1(mid)) ans=mid,r=mid-1; else l=mid+1; } cout<<ans<<'\n'; } else {//h*(h+1)/2 < n ll l=h,r=2e9,ans,mid; while(l<=r){//找到最小的符合条件的mid mid=l+r>>1; if(judge2(mid)) ans=mid,r=mid-1; else l=mid+1; } cout<<ans<<'\n'; } }