Sand Fortress
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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  |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
  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 5spots 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, ...]

【题意】

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

【思路】

只求总个数,所以不必关心具体的序列。假设现在要分析总和为n,k个数的序列是否存在,就得去分解n,这件事是很难完成的,不如换一个角度,反向分析:所有满足“首项≤H≤H,相邻两项差不大于1,最后一项是1”的k个数的序列当中,有没有总和刚好是n的呢?

很容易发现 ,把kk定下来以后,一个满足条件的序列的总和,它的取值是一个连续的范围,而且下界明显是kk,那么上届是多少呢?

在kk个位置中填入最大的数,根据奇偶的不同,可能有下面两种情况,右边一定是黄色所示的三角形。如果n比较小可能只有黄色部分。

只要kk个数的和的区间[k,maxSUM][k,maxSUM]中包含nn,kk就是可行的。而且因为kk越大,maxSUM就越大,所以如果kk可行,那么k+1k+1也一定可行,正是因为这种单调性,可以kk进行二分查找。

【注意】

由于n, H比较i大,可能会出现乘积结果long long 溢出。为了避免在1~n进行二分,考虑一个更好的上届。我使用的下面的这种摆放的方法得到的上届:(k/2)2=n(k/2)2=n,所以最多用k=2√nk=2n个数

  1. #include<stdio.h>
  2. #include<math.h>
  3. typedef long long ll;
  4. ll n, H;
  5. //绝对值
  6. #define mabs(x) ((x)>0?(x):(0-(x)))//a, a+1, ...,b连续求和
  7. #define SUM(a,b) (a+b)*(mabs(b-a)+1)/2
  8. ll check(ll len)
  9. {//检查三种情况,求maxSUM并于n比较
  10. ll area;
  11. if (len <= H)
  12. area = SUM(, len);
  13. else
  14. {
  15. area = SUM(, H-);
  16. len -= H;
  17. if (len & )
  18. area = area + SUM(H, H + len / ) + SUM(H + len / , H);
  19. else
  20. area = area + SUM(H, H + len / ) + SUM(H + len / - , H);
  21. }
  22. return area >= n;
  23. }
  24.  
  25. //二分查找k
  26. ll binsch(ll fr,ll to)
  27. {
  28. ll l = fr - , r = to + , m;
  29. while (l+<r)
  30. {
  31. m = (l + r) / ;
  32. if (check(m)!= )l = m;
  33. else r = m;
  34. }
  35. return r;
  36. }
  37.  
  38. int main()
  39. {
  40. scanf("%I64d %I64d", &n,&H);
  41. ll r = *sqrt(n)+;
  42. ll ans=binsch(, r);
  43. printf("%I64d", ans);
  44. }

codeforces 985 D. Sand Fortress(二分+思维)的更多相关文章

  1. Codeforces 985 D - Sand Fortress

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

  2. codeforces 799 C. Fountains(二分+思维)

    题目链接:http://codeforces.com/contest/799/problem/C 题意:要求造2座fountains,可以用钻石,也可以用硬币来造,但是能用的钻石有限,硬币也有限,问能 ...

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

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

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

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

  5. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  6. CodeForces 985D Sand Fortress

    Description You are going to the beach with the idea to build the greatest sand castle ever in your ...

  7. codeforces E. Mahmoud and Ehab and the function(二分+思维)

    题目链接:http://codeforces.com/contest/862/problem/E 题解:水题显然利用前缀和考虑一下然后就是二分b的和与-ans_a最近的数(ans_a表示a的前缀和(奇 ...

  8. codeforces 814 C. An impassioned circulation of affection(二分+思维)

    题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...

  9. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

随机推荐

  1. java web 实体类生成

    工具下载地址:https://download.csdn.net/download/g342105676/10813246

  2. Angular 2 Architecture Overview

    Module 简单来说模块(module)就是完成共同目的的代码块,export一些内容例如一个类.函数.或值变量. component就是一个基本的Angular块,一个component类其实也是 ...

  3. python 爬虫003-正则表达式简单介绍

    正则表达式,简单的说就是用一个“字符串”来描述一个特征,然后去验证另外一个“字符串”是否符合这个特征. 正则表达式在线测试工具 http://tool.chinaz.com/regex 实例一,判断字 ...

  4. git rm -r --cached 去掉已经托管在git上的文件

    1.gitignore文件 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则例如: # 此为注释 – 将被 ...

  5. Node.js权威指南学习记录

    学习nodeJS权威指南的学习记录 导航: 1.console模块 2.全局变量 3.Buffer对象 4.事件对象 5.网络请求 6.文件操作对象 一. COMMON.js的学习.(commonJS ...

  6. Prism5.0新内容 What's New in Prism Library 5.0 for WPF(英汉对照版)

    Prism 5.0 includes guidance in several new areas, resulting in new code in the Prism Library for WPF ...

  7. Xcode And iOS9新特性

    Xcode And iOS9 1. Xcode7 新特性 > 可直接在真机上运行自己的应用,只需要有苹果账号,无需购买苹果开发者账号. > 可设置在出现 EXC_BAD_ACCESS 错误 ...

  8. 发现的好东西——bitset

    先向各位大佬介绍一个水题 任何一个正整数都可以用2的幂次方表示.例如 137=2^7+2^3+2^0 同时约定方次用括号来表示,即a^b 可表示为a(b). 由此可知,137可表示为: 2(7)+2( ...

  9. Windows10重启之后总是将默认浏览器设置为IE

    换了一台电脑之后,发现系统重启之后总是会把我的默认浏览器设置为IE,而自从用上了Chrome,我对他爱不释手. 上网找了不少文章,都建议使用系统自带的设置进行默认浏览器的设置,试了三四次,完全不起任何 ...

  10. 转载的,讲解java.util的集合类

    本文是转载的 http://www.ibm.com/developerworks/cn/java/j-lo-set-operation/index.html#ibm-pcon 在实际的项目开发中会有很 ...