CodeForces 985D Sand Fortress

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

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

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。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, h;
  scanf("%I64d%I64d", &n, &h);
  if ((n * 2 + h) / (h + 1) <= h) {
    ll l = 1, r = h;
    while (l < r) {
      ll mid = (l + r + 1) / 2;
      if (2 * n / mid >= mid + 1) l = mid;
      else  r = mid - 1;
    }
    printf("%I64d\n", l + ((2 * n + l - 1) / l > l + 1));
  } else {
    if (n <= h * (h + 1) / 2 + h) {
      printf("%I64d\n", h + 1);
      return 0;
    }
    n -= (h - 1) * h / 2;
    ll l = 0, r = (ll)sqrt(n) + 1;
    while (l < r) {
      ll mid = (l + r + 1) / 2;
      if ((n + mid) / (mid + 1) > (2 * h + mid))  l = mid;
      else  r = mid - 1;
    }
    ll ans = h - 1 + 2 * (l + 1);
    n -= (l + 1) * (2 * h + l);
    assert(n >= 1 && n <= 2 * (h + l + 1));
    if (n <= h + l + 1) ans += 1;
    else  ans += 2;
    printf("%I64d\n", ans);
  }
  return 0;
}

原文地址:https://www.cnblogs.com/hitgxz/p/9977652.html

时间: 2024-10-10 02:28:57

CodeForces 985D Sand Fortress的相关文章

Codeforces 985 D - Sand Fortress

D - Sand Fortress 思路: 二分 有以下两种构造, 分别二分取个最小. 代码: #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #defi

Educational Codeforces Round 44#985DSand Fortress+二分

传送门:送你去985D: 题意: 你有n袋沙包,在第一个沙包高度不超过H的条件下,满足相邻两个沙包高度差小于等于1的条件下(注意最小一定可以为0),求最少的沙包堆数: 思路: 画成图来说,有两种可能,一种是y=h-x一次函数和常函数y=x组合,还有一种是先上升后下降的函数,注意斜率绝对值都是1: 二分答案,具体来说,我是二分了最大高度,主要是check()比较要思考,(昨天晚上没有细心写check,错过了很多AC:): 这个check中:如果最大高度 mid 比m小,说明不会有折线,直接考虑三角

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

C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles. At the end of the

Codeforces Round #302 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/544 A. Set of Strings time limit per test:1 second memory limit per test:256 megabytes You are given a string q. A sequence of k strings s1,?s2,?...,?sk is called beautiful, if the concatenation of these strings is

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store