CodeForces 985D Sand Fortress

探讨了在无限长的海滩线上,如何使用有限数量的沙包,遵循特定规则,建造最紧凑的沙堡。算法通过二分查找确定沙堡的最优结构,确保沙堡高度平滑过渡且不超过左侧围栏的高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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 described 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 \(h_i\) 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:

  • \(h_1 \le H\) : no sand from the leftmost spot should go over the fence;
  • For any \(i \in \left[1, \infty\right)\), \(|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;
  • \(\sum_{i=1}^{\infty}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 \le n, H \le 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, ...]

Solution

根据样例理解一下题意,就是给定\(n\)\(H\),要找到一个无限长的序列\(h_{1}, h_{2}, h_{3}, \dots\),满足:

  • \(h_{1} \le H\)
  • \(\forall i \ge 0, \left|h_{i} - h_{i+1}\right| \le 1\)
  • 存在一个\(N\),当\(i \ge N\)时,\(h_i = 0\)

我们的任务是找到一个满足上述三个条件的序列,使得序列中的非零元素最少。

最优的答案或者是一个从某个值递减到1的序列,或者是一个先从H​递增,再递减到1的序列,分情况处理。

对于第一种情况,通过二分找到一个递减的初始值,具体来讲,就是找到最大的满足\(\sum_{i=1}^{h}i \le n\)\(h\),如果\(n = \sum_{i=1}^{h}i\),则答案为\(h\),否则答案为\(h + 1\)

对于第二种情况,我是这样考虑的,首先序列的尾部是\(H-1, H-2, \dots, 1, 0, 0, \dots\),然后在序列的头部插入\(2 \times H, 2 \times (H + 1), 2 \times (H + 2), \dots\),我们可以通过二分找到一个最大的满足\(\sum_{i=1}^{H-1}i + 2\sum_{i=0}^{h}(H+i) \le n\)\(h\),再简单讨论一下。

大致的思路是这样的,具体如何二分因人而异,这道题的数据范围比较大,所以判断条件要写得小心一些,避免爆long long。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, h;
  scanf("%I64d%I64d", &n, &h);
  if ((n * 2 + h) / (h + 1) <= h) {
    ll l = 1, r = h;
    while (l < r) {
      ll mid = (l + r + 1) / 2;
      if (2 * n / mid >= mid + 1) l = mid;
      else  r = mid - 1;
    }
    printf("%I64d\n", l + ((2 * n + l - 1) / l > l + 1));
  } else {
    if (n <= h * (h + 1) / 2 + h) {
      printf("%I64d\n", h + 1);
      return 0;
    }
    n -= (h - 1) * h / 2;
    ll l = 0, r = (ll)sqrt(n) + 1;
    while (l < r) {
      ll mid = (l + r + 1) / 2;
      if ((n + mid) / (mid + 1) > (2 * h + mid))  l = mid;
      else  r = mid - 1;
    }
    ll ans = h - 1 + 2 * (l + 1);
    n -= (l + 1) * (2 * h + l);
    assert(n >= 1 && n <= 2 * (h + l + 1));
    if (n <= h + l + 1) ans += 1;
    else  ans += 2;
    printf("%I64d\n", ans);
  }
  return 0;
}

转载于:https://www.cnblogs.com/hitgxz/p/9977652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值