题意:
有容量为n的谷仓,每天先进m谷粒,但由于有麻雀偷食,第i天有i个麻雀偷食,如果一个麻雀偷一粒谷粒,那么在第几天谷仓会第一次为0个谷粒?
这题就用二分解就行,直接找到符合条件的那一天。
注意:1. 如果每天加入的谷粒大于谷仓的容量,那么当麻雀数等于谷仓容量的那天谷仓会空;
2. 谷粒小于谷仓容量时,那么从第m+1天开始,谷仓以n-m的数量进行减1,减2,减3 ... ...;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
using namespace std;
int main()
{
long long n,m;
scanf("%lld%lld",&n,&m);
if(n<=m)
cout<<n<<endl;
else
{
long long int k=n-m;
long long l=1,r=5e9+7;
while(l<r)
{
long long int mid=(l+r)/2;
if(mid*(mid+1)/2>=k)
r=mid;
else
l=mid+1;
}
long long ans=l+m;//记得将之前忽视的m天加上;
cout<<ans<<endl;
}
return 0;
}