题干:

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

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, ...]

题目大意:

对给定的n,H,把n划分为a1,a2,a3,...a1,a2,a3,...,要求首项a1≤Ha1≤H,相邻两项之差不大于1,而且最后一项必须是1。总个数要最少,输出这个最小的总个数。

解题报告:

附一个比较好的题解链接

总的来说,就是先把能填的填上,然后剩下的按照正态分布填(这样可以保证最大)二分验证这个可以可行即可。注意会爆longlong所以给出两种处理方式。

AC代码1:

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll n,h;
bool ok(ll x) {ll ans=0;if(x>=2e9) return 1;if(x<=h) ans=x*(x+1)/2;else {ans=h*(h+1)/2+(x-h)*h;   ll high=x-h-1;ll t1=high/2,t2=high-t1;ans+=t1*(t1+1)/2+t2*(t2+1)/2;}if(ans>=n)return 1;else return 0;
}
int main()
{cin>>n>>h;ll l=1,r=1e18+1;ll mid=(l+r)/2;while(l<r) {mid = (l+r)/2;if(ok(mid)) r=mid;else l=mid+1;}printf("%lld\n",l);return 0;
}

AC代码2:

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll n,h;
bool ok(ll x) {ll ans=0;
//  if(x>=2e9) return 1;if(x<=h) ans=x*(x+1)/2;else {ans=h*(h+1)/2+(x-h)*h;    ll high=x-h-1;ll t1=high/2,t2=high-t1;ans+=t1*(t1+1)/2+t2*(t2+1)/2;}if(ans>=n) return 1;else return 0;
}
int main()
{cin>>n>>h;ll l=1,r=2*sqrt(n)+1;ll mid=(l+r)/2;while(l<r) {mid = (l+r)/2;if(ok(mid)) r=mid;else l=mid+1;}printf("%lld\n",l);return 0;
}

总结:难啊!!难吗??其实还是做题经验太少了。。。。

【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)相关推荐

  1. codeforces+contest+985D. Sand Fortress+思维

    二分枚举长度 去算这一个点左边有多少个 D. Sand Fortress time limit per test 2 seconds memory limit per test 256 megabyt ...

  2. Educational Codeforces Round 53C(二分,思维|构造)

    #include<bits/stdc++.h> using namespace std; const int N=1e6+6; int x[N],y[N]; int sx,sy,n; ch ...

  3. 51nod 1574 || Codeforces 584 E. Anton and Ira 思维+构造+贪心

    传送门:E. Anton and Ira 题意:给定两个1-n的全排列p和s,假设交换pi和pj的代价是abs(i-j),问怎样将p交换成s才能使代价最小. 思路:先将s映射成1-n的顺序排列,再将p ...

  4. CodeForces - 507E Breaking Good(二分+贪心)

    题目链接:点击查看 题目大意:给出 n 个竹子,初始时高度为 h[ i ],接下来每一天每个竹子都会长高 a[ i ] 个单位的高度,每一天可以砍 k 次竹子(可以是同一个),每次可以砍掉 p 个单位 ...

  5. CodeForces - 363D Renting Bikes(二分+贪心)

    题目链接:点击查看 题目大意:给出n个人,每人有元钱,再给出m辆自行车,每辆车需要元钱才能骑,现在n个人有a元的共享资金,问最多可以骑多少量自行车,并且使每个人的花费总和最小(即尽可能多的使用共享资金 ...

  6. CodeForces - 715A Plus and Square Root(思维+构造)

    题目链接:点击查看 题目大意:我们在玩一个游戏,屏幕上有一个数字,我们设这个数字为x,初始值为2,我们一开始的等级是k,我们每一次可以有两种操作: 加法:可以让x加上k 开根号:可以让x开根号,并且等 ...

  7. 【CodeForces - 483C】Diverse Permutation(思维构造)

    题干: Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct posit ...

  8. Codeforces 893 D Credit Card 贪心 思维

    题目链接: http://codeforces.com/contest/893/problem/D 题目描述: 每天晚上一个数值a,如果a>0表示今天收入a元,<0表示亏损a元,=0表示去 ...

  9. 【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

    题干: Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wa ...

  10. 【CodeForces - 260A】Adding Digits (思维构造)

    题干: Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to rep ...

最新文章

  1. Wiener Filter
  2. Android 6.0 7.0 8.0 一个简单的app内更新版本-okgo app版本更新
  3. MySQL where后面的标量子查询使用
  4. Tp3.1 文件上传到七牛云
  5. linux下安装dovecot
  6. 阿里云服务器被[kthreaddi]挖矿病毒攻击
  7. win10子系统 php,启用 Win10 的 Linux 子系统
  8. mysql被更新失败_更新mysql出错:出错原因 You are using safe update mode
  9. 一种快速部署开发用oracle的办法
  10. 51nod-1562:玻璃切割(O(n)模拟)
  11. javaweb——jsp(学习总结,javaweb必备技能)
  12. LINUX SHELL判断一个用户是否存在
  13. 如何检测Linux内核的Rootkit
  14. Table [xx] contains physical column name referred to by multiple physical column names 错误处理
  15. android中AudioRecord使用详解
  16. html 圆角矩形,圆角矩形
  17. 淘宝中的UV,PV,IPV
  18. PHP快手直播弹幕采集,获取斗鱼弹幕php版(原创)
  19. python中的scipy库_scipy库中的odeint函数
  20. 对excel表格按照某个字段拆分

热门文章

  1. [Leedcode][JAVA][第999题][直接考虑题意]
  2. [剑指offer]面试题第[3]题[JAVA][从尾到头打印链表][栈]
  3. CF#420 B. Okabe and Banana Trees 思维|暴力|几何
  4. java jxl 写 excel文件_Java使用jxl写入Excel文件
  5. 电子报账系统源码_网上商城系统建设心得,轻松搞定选择困难
  6. jsp中设置自动换行_办公技巧—Word中如何设置自动生成序号
  7. java multimap 序列化_C++ JSON库的使用
  8. 3485. 最大异或和
  9. android studio内置终端,Android Studio Terminal xx不是内部或者外部命令
  10. c语言编程怎么实现替换,使用C语言实现字符串中子字符串的替换