思路:二分,.有以下两种构造,
分别计算两种情况取个最小。
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
ll n, H;
bool check1(ll t)
{
ll cnt = 0;
if(t <= H) cnt = t * (t + 1) / 2;
else cnt = t * (t + 1) / 2 + t * (t - 1) / 2 - H * (H - 1) / 2; //H ->H+1->....t->t-1->....1
return cnt >= n;
}
bool check2(ll t)
{
ll cnt = 0;
if(t < H) cnt = t * (t + 1) / 2;
else cnt = t * (t + 1) / 2 + (H + t) * (t - H + 1) / 2;//H ->H+1->....t->t->t-1->....1
return cnt >= n;
}
int main()
{
scanf("%lld %lld", &n, &H);
ll res = 1LL<<60, ans;
ll st = 1, ed = 2e9;
while(st <= ed) {
ll mid = (st + ed) >> 1;
if(check1(mid)) {
res = min(res, mid);
ed = mid - 1;
} else {
st = mid + 1;
}
}
if(res >= H) ans = 2 * res - H;
else ans = res;
st = 1, ed = 2e9;
while(st <= ed) {
ll mid = (st + ed) >> 1;
if(check2(mid)) {
res = min(res, mid);
ed = mid - 1;
} else {
st = mid + 1;
}
}
if(res >= H) ans = min(ans, 2 * res - H + 1);
else ans = min(ans, res);
printf("%lld\n", ans);
return 0;
}