博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 985 D. Sand Fortress(二分+思维)
阅读量:4633 次
发布时间:2019-06-09

本文共 3544 字,大约阅读时间需要 11 分钟。

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
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 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,a2a3,...a1,a2,a3,...,要求首项a1Ha1≤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=2nk=2n个数

 

#include
#include
typedef long long ll;ll n, H;//绝对值#define mabs(x) ((x)>0?(x):(0-(x)))//a, a+1, ...,b连续求和#define SUM(a,b) (a+b)*(mabs(b-a)+1)/2ll check(ll len) {
//检查三种情况,求maxSUM并于n比较 ll area; if (len <= H) area = SUM(1, len); else { area = SUM(1, H-1); len -= H; if (len & 1) area = area + SUM(H, H + len / 2) + SUM(H + len / 2, H); else area = area + SUM(H, H + len / 2) + SUM(H + len / 2 - 1, H); } return area >= n;} //二分查找kll binsch(ll fr,ll to) { ll l = fr - 1, r = to + 1, m; while (l+1

 

转载于:https://www.cnblogs.com/caiyishuai/p/9082830.html

你可能感兴趣的文章
Ruby: 获取IE的一些信息(其实应用AutoIt脚本本身,获取这些信息更加简单)
查看>>
微信小程序之动态获取元素宽高
查看>>
request.setAttribute
查看>>
HDU 1281 二分图
查看>>
CF2.C
查看>>
NYOJ 832 DP
查看>>
Beta版本发布
查看>>
表单提交验证方法
查看>>
JS框架设计读书笔记之-核心模块
查看>>
实验二
查看>>
bind(),call(), apply()方法的区别是什么?
查看>>
android--开机启动--在某些机型上开机不能启动的问题
查看>>
通过 Storyboard 快速搭建一系列连贯性的视图控制器
查看>>
2-3 Sass的函数功能-列表函数
查看>>
更改oracle字符集 error: ora-12712 解决方法
查看>>
mysql 报错 ERROR 1101 (42000): BLOB/TEXT column can’t have a default value
查看>>
JAVA设计模式-工厂模式(代码示例)
查看>>
根据会计期间获取月度第一天,最后一天和年度第一天
查看>>
PostgreSQL安装日志
查看>>
.NET中锁
查看>>