码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 985 D. Sand Fortress

时间:2018-05-22 12:44:53      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:dimens   really   and   wan   cond   byte   scanf   nbsp   efi   

D. 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 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,构造一个无穷项序列,$$$a_1, a_2,a_3, ..., a_{/infty}$$$,要求首项$$$a_1 \le H$$$,相邻两项之差不大于1,所有项总和为n。

 

【思路】

 

#include<stdio.h>
#include<math.h>
typedef long long ll;
ll n, H;
#define mabs(x) ((x)>0?(x):(0-(x)))
#define SUM(a,b) (a+b)*(mabs(b-a)+1)/2
ll check(ll len) {
	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;
}

ll binsch(ll fr,ll to) {
	ll l = fr - 1, r = to + 1, m;
	while (l+1<r)
	{
		m = (l + r) / 2;
		if (1!=check(m) )l = m;
		else r = m;
	}
	return r;
}



int main() {
	scanf("%lld %lld", &n,&H);
	ll r = 2*sqrt(2*n)+1;
	ll ans=binsch(1, r);
	printf("%lld", ans);
}

codeforces 985 D. Sand Fortress

标签:dimens   really   and   wan   cond   byte   scanf   nbsp   efi   

原文地址:https://www.cnblogs.com/tobyw/p/9070846.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!