cf Educational Codeforces Round 44 D. Sand Fortress

原題:
D. Sand Fortress
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 i[1,)|hihi+11| 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;
i=1hi=n 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 ≤ 10^18) — 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
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, …]

中文:

給你兩個數和H,現在讓你把n分成若干份,首項不能超過H,每相鄰的兩份之間差的絕對值不超過1,而且最後一份必須是1。問你最少分成多少份?

代碼:

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

ull n,h;


ull get_tot(ull s,ull e)//s到e求累加和
{
    if(s>e)
        swap(s,e);
    ull tmp;
    if((s+e)%2)
    {
        tmp=(e-s+1)/2;
        return tmp*(s+e);
    }
    else
    {
        tmp=(s+e)/2;
        return tmp*(e-s+1);
    }
}

bool C(ull x)
{
    ull res=0;
    if(x<=h)
    {
       res=get_tot(1,x);
    }
    else
    {
        res=get_tot(1,h-1);

        x-=h;

        res+=get_tot(h,h+x/2);

        if(x%2)
            res+=get_tot(h+x/2,h);
        else
            res+=get_tot(h+x/2-1,h);
    }

    return res>=n;
}

ull solve(ull l,ull r)
{
    ull mid;
    while(r-l>1)
    {
        mid=(l+r)/2;

        if(C(mid))
            r=mid;
        else
            l=mid;
    }
    return r;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>h)
    {
        ull r=1+sqrt(n)*2;//最大值上界是對稱的情況,否則數值溢出
        cout<<solve(0,r)<<endl;
    }
    return 0;
}







思路:

直接用貪心的方式去硬分解n是不行的。但是,如果知道k的值,就可以按照首項不超過大H的要求,得到最多和最少的份數,只要判斷n是否在這個區間當中,如果n在這個區間當中,那麼一定可以有k份組成n。

那麼就可以用二分的方法來枚舉k即可。

關鍵在於枚舉到k了以後如何去分呢?

如果當前枚舉到的k值小於H,那麼得到的k份一定是從1一直累加到k。

如果當前枚舉到的k值大於H,那麼可以讓第一份的值爲H,第二份的值爲H+1一直到H+x,然後變成H+x-1,H+x-2…到H最後變成1。即爲一個先增加後減少的過程。(此處分奇數和偶數分開計算)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章