1. 程式人生 > >Codeforces 985D(二分答案)

Codeforces 985D(二分答案)

傳送門

題面:

D. Sand Fortress

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ibe 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

Copy

5 2

output

Copy

3

input

Copy

6 8

output

Copy

3

Note

Here are the heights of some valid castles:

  • n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...]
  • n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)

The first list for both cases is the optimal answer, 3 spots are occupied in them.

And here are some invalid ones:

  • n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...]
  • n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]

題目描述:

    你有n堆沙子,最左邊的沙子的最大高度不能超過H,讓你在一個從1到正無窮的一維平面內放沙子,且要滿足相鄰兩個座標的沙子的高度不能超過1。問沙子能夠佔用的最少座標點的個數。

題目分析:

    這個題實質上有一個隱藏條件,因為題目要求相鄰兩個座標的沙子高度不能超過1,這代表最後一堆沙子的個數也必定為1。而題目要我們求佔用座標點的最少個數,根據貪心的思想,要使佔用的點最少,則我們必須要貪心地令沙子的高度儘可能高。然而又因為最後一堆的沙子的個數為1,因此我們可以判斷出,沙子的高度所形成的曲線必定是先增後減的或者一直減小的曲線。即——沙子的高度存在單調性!因此我們就可以通過二分答案的方法,不斷二分佔用座標的個數即可。

    而二分的check函式,我們因為最優的情況下,兩兩沙堆相差均為1,因此我們可以運用等差數列求和公式求出總的沙子堆數,並用它與n進行對比今兒二分即可。

    值得一提的是,當枚舉出n為奇數的時候,會出現有兩個座標的值均相當的情況,需要格外留意!

程式碼:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,h;
const ll cnt=2000000000;
bool check(ll x){
    if(x<=h){
        if(x>=cnt) return true;
        return x*(x+1)/2>=n;
    }
    ll a=(x-h)/2+h;
    if(a>=cnt) return true;
    if((x-h)%2!=0)
    return a*(a+1)/2+(a+h)*(a+1-h)/2>=n;
    return a*(a+1)/2+(a-1+h)*(a-h)/2>=n;
}
int main()
{
    cin>>n>>h;
    ll ln=1,rn=n;
    while(ln<=rn){
        ll mid=(ln+rn)>>1ll;
        if(check(mid)) rn=mid-1;
        else ln=mid+1;
    }
    cout<<rn+1<<endl;
}