cf Educational Codeforces Round 44 D. Sand Fortress

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tengfei461807914/article/details/81538540

原题:
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 , ) | h i h i + 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;
i = 1 h i = 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。即为一个先增加后减少的过程。(此处分奇数和偶数分开计算)

猜你喜欢

转载自blog.csdn.net/tengfei461807914/article/details/81538540