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

  1. 5 2

output

  1. 3

input

  1. 6 8

output

  1. 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。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int main() {
  5. ll n, h;
  6. scanf("%I64d%I64d", &n, &h);
  7. if ((n * 2 + h) / (h + 1) <= h) {
  8. ll l = 1, r = h;
  9. while (l < r) {
  10. ll mid = (l + r + 1) / 2;
  11. if (2 * n / mid >= mid + 1) l = mid;
  12. else r = mid - 1;
  13. }
  14. printf("%I64d\n", l + ((2 * n + l - 1) / l > l + 1));
  15. } else {
  16. if (n <= h * (h + 1) / 2 + h) {
  17. printf("%I64d\n", h + 1);
  18. return 0;
  19. }
  20. n -= (h - 1) * h / 2;
  21. ll l = 0, r = (ll)sqrt(n) + 1;
  22. while (l < r) {
  23. ll mid = (l + r + 1) / 2;
  24. if ((n + mid) / (mid + 1) > (2 * h + mid)) l = mid;
  25. else r = mid - 1;
  26. }
  27. ll ans = h - 1 + 2 * (l + 1);
  28. n -= (l + 1) * (2 * h + l);
  29. assert(n >= 1 && n <= 2 * (h + l + 1));
  30. if (n <= h + l + 1) ans += 1;
  31. else ans += 2;
  32. printf("%I64d\n", ans);
  33. }
  34. return 0;
  35. }

CodeForces 985D Sand Fortress的更多相关文章

  1. Codeforces 985 D - Sand Fortress

    D - Sand Fortress 思路: 二分 有以下两种构造, 分别二分取个最小. 代码: #include<bits/stdc++.h> using namespace std; # ...

  2. codeforces 985 D. Sand Fortress(二分+思维)

    Sand Fortress time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. CF985D Sand Fortress

    思路: 很奇怪的结论题,不好想.参考了http://codeforces.com/blog/entry/59623 实现: #include <bits/stdc++.h> using n ...

  4. Codeforces 985D

    题意略. 思路:这个题本来打算先推一下公式,然后解方程来算.函数图像大概如下: 最左端为H.但是由于中间那个尖的地方(假设它的高度为h),可能在那个地方有多堆沙包,所以推公式貌似不行. 但是最高高度h ...

  5. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  6. Codeforces 985 最短水桶分配 沙堆构造 贪心单调对列

    A B /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a, ...

  7. Educational Codeforces Round 44#985DSand Fortress+二分

    传送门:送你去985D: 题意: 你有n袋沙包,在第一个沙包高度不超过H的条件下,满足相邻两个沙包高度差小于等于1的条件下(注意最小一定可以为0),求最少的沙包堆数: 思路: 画成图来说,有两种可能, ...

  8. Codeforces Round #355 (Div. 2)-C

    C. Vanya and Label 题目链接:http://codeforces.com/contest/677/problem/C While walking down the street Va ...

  9. Codeforces 599C Day at the Beach(想法题,排序)

    C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...

随机推荐

  1. 将DataRow赋值给model中同名属性

    /// <summary> /// 将DataRow赋值给model中同名属性 /// </summary> /// <typeparam name="T&qu ...

  2. MySQL Bug导致异常宕机的分析流程

    原文链接:http://click.aliyun.com/m/42521/ 摘要: 本文主要通过一个bug来记录一下如何分析一个MySQL bug的崩溃信息. 版本:Percona 5.7.17-11 ...

  3. 转:线程Thread (1)

    引言 1.理解多线程 2. 线程异步与线程同步 3.创建多线程应用程序 3.1通过System.Threading命名空间的类构建 3.1.1异步调用线程 3.1.2并发问题 3.1.3线程同步 3. ...

  4. 安卓 USB摄像头 开源库 UVCCamera 教程

    https://github.com/saki4510t/UVCCamera UVCCamera 听名字就知道使用UVC( USB VEDIO CLASS) 协议的通用类库.linux原生支持,基本支 ...

  5. 发现微信支付bug

    第一张银行卡支付金额不足无法付款,选择另一张同样密码的银行卡,居然不用重新输入密码即可直接付款成功!

  6. 企业大数据之Elasticsearch的搜索类型

    下面的 ES基于版本(V2.3.4) ES之默认 1.默认自动发先同一局域网的所有集群节点 2.默认一个索引库会有5个分片,(分片越多,效率越好) 由于这两个默认,所以统一索引库的分片对分布在不同机器 ...

  7. in有两种用法:

    # in有两种用法: 1. 在for中. 是把每一个元素获取到赋值给前⾯的变量. 2. 不在for中. 判断xxx是否出现在str中. #len() 为内置函数,输出为1,2,3,4....., 长度 ...

  8. 工作中碰到的一个问题(cookie相关)

    今天上线了一个API,6台机器做的集群.API的第一步是读取cookie,判断用户是否登录. 例如,线上服务器分别是 10.255.242.1 10.255.242.2 10.255.242.3 10 ...

  9. jQuery复制table header到表格的最下面

    为了让table具有更好的可读性,我们可以将表格的header信息克隆一份到表格的底部,这种特效通过JQuery就很容易实现: 1 2 3 4 5 var $tfoot = $(''); $($('t ...

  10. 使用 libffi 实现 AOP

    核心还是利用oc消息的查找派发机制,进行类结构的动态修改,用新函数替换老函数,然后再调用老函数. 前言     众所周知,使用runtime的提供的接口,我们可以设定原方法的IMP,或交换原方法和目标 ...