D - Sand Fortress
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height hi of the sand pillar on some spot i be the number of sand packs you spent on it. You can’t split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle:
h1 ≤ H: no sand from the leftmost spot should go over the fence;
For any |hi - hi + 1| ≤ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don’t want this to happen;
: you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
Input
The only line contains two integer numbers n and H (1 ≤ n, H ≤ 1018) — the number of sand packs you have and the height of the fence, respectively.
Output
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
Examples
Input
5 2
Output
3
Input
6 8
Output
3
二分法,注意条件的判断要分两种情况,d>h和的<=h时,并应用等差数列求和公式。
代码来源:https://blog.csdn.net/Yi_Qing_Z/article/details/80935468
在这里插入代码片
```#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#include<map>
#include<sstream>
#include<set>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define CLE(a,b) memset(a,b,sizeof(a))
#define MEC(a,b) memcpy(a,b,sizeof(a))
#define ll long long
#define inff 0x7fffffff
#define mabs(x) (x>0?x:(0-(x)))
#define SUM(a,b) ((a+b)*(mabs(b-a)+1)/2)
//const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
//const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
//const int dx[9] = { 0,0,1,-1 };
//const int dy[9] = { 1,-1,0,0 };
//struct {
//};
using namespace std;
ll n, h;
int judge(ll d)
{
ll area;
if (d <= h)
{
area = SUM(1, d);
}
else
{
area = SUM(1, h - 1);
d -= h;
if (d & 1)
area = area + SUM(h, h + d / 2) + SUM(h + d / 2, h);
else
area = area + SUM(h, h + d / 2) + SUM(h + d / 2 - 1, h);
}
return area >= n;
}
ll bise(ll fr, ll r)
{
ll lef = fr - 1;
r = r + 1;
while (lef + 1 < r)
{
ll m = (lef + r) >> 1;
if (1 != judge(m))
{
lef = m;
}
else
r = m;
}
return r;
}
int main()
{
cin >> n >> h;
ll ri = 2*sqrt(n)+1;
ll ans = bise(1, ri);
cout << ans << endl;
return 0;
}