CF-Educational Codeforces Round 44 (Rated for Div. 2)-D-Sand Fortress
阿新 • • 發佈:2018-12-23
描述
題解
題不是很容易理解,求滿足題中所給三個條件的最少建立沙堡的方案。可以用二分解,不過這個題需要注意的是,會爆掉 ,需要用到 型別來做比較才行。
程式碼
#include <iostream>
using namespace std;
typedef long long int ll;
double cal(double x)
{
return x * (x + 1) / 2;
}
ll n, H;
bool check(ll m)
{
if (m <= H)
{
return cal(m) >= n;
}
double cnt = cal(H) + H;
ll x = m - H - 1;
cnt += H * x;
if (x % 2 == 0)
{
cnt += 2 * cal(x / 2);
}
else
{
cnt += cal(x / 2) + cal((x + 1) / 2);
}
return cnt >= n;
}
int main()
{
cin >> n >> H;
ll l = 1 , h = n;
while (l + 1 < h)
{
ll m = (l + h) >> 1;
if (check(m))
{
h = m;
}
else
{
l = m;
}
}
cout << h << endl;
return 0;
}