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

题干:

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441428.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql多实例脚本_mysql多实例脚本

mysql多实例脚本##mariadb和mysql-server的通用多实例脚本。vi mdp.sh 脚本内容参考内容如下#!/bin/bashecho ‘等待mariadb-server或mysql-server服务软件安装完毕’while truedoyum install -y mariadb-server mariadb &>/dev/nullyum install -y mysql-serv…

scrapy 分布式 mysql_Scrapy基于scrapy_redis实现分布式爬虫部署的示例

准备工作1.安装scrapy_redis包,打开cmd工具,执行命令pip install scrapy_redis2.准备好一个没有BUG,没有报错的爬虫项目3.准备好redis主服务器还有跟程序相关的mysql数据库前提mysql数据库要打开允许远程连接,因为mysql安装后root用户默认只允许本地连接,详情请看此文章部署过程…

(精)DEVC++的几个实用小技巧

依赖 DEV C 5.11 最新版 下载安装DEV C后&#xff0c;使用DEV C打开一个随便的cpp文件&#xff0c;你看到的应该是这样的界面。&#xff08;为了节约读者的流量&#xff0c;图片进行了有损压缩&#xff0c;但是字看得清楚&#xff09; 重点是确认工具栏有AStyle选项。 相信…

win10一按右键就闪屏_升级Win10正式版后屏幕一直闪烁正确的解决办法

Win10正式版屏幕一直闪烁怎么办呢&#xff1f;升级到Win10正式版并进入Windows桌面后&#xff0c;发现屏幕一直不断的闪烁&#xff0c;此时无法执行任务操作。小编最近在升级到Win10正式版后才遇到了这个问题&#xff0c;后台经过反复思考和探索&#xff0c;终于解决了问题&…

KMP 常用模板

写在前面&#xff1a; 代码中有的是i&#xff0c;j&#xff0c;有的是j&#xff0c;k&#xff0c;总之不唯一啊&#xff01;&#xff01;&#xff01;这个一定要弄清楚&#xff01;&#xff01;&#xff01;我没有统一。。 从0开始的字符串&#xff0c;加速版。 void getnext…

在spark的软件栈中_问:Spark Streaming是什么软件栈中的流计算?

问&#xff1a;Spark Streaming是什么软件栈中的流计算?A:不能确定,B:,C:,D:正确答案:绛旓細姣斿皵路鐩栬尐解析&#xff1a;问&#xff1a;Spark Streaming是什么软件栈中的流计算?A:不能确定,B:,C:,D:相关问题&#xff1a;“逻辑与”的运算符是( )。A:&&B:&C:…

【CodeForces - 195A】Let's Watch Football (追及问题,模拟)

题干&#xff1a; Valeric and Valerko missed the last Euro football game, so they decided to watch the games key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, i…

下面不是mysql特性_下面( )不是MySQL的特性。_学小易找答案

【单选题】如何检测 2型糖尿病【填空题】<5>以下do-while语句中循环体的执行次数是( ). a10; b0; do { b2; a-2b; } while (a>0);【单选题】2 型糖尿病 多 发在什么年龄段【填空题】<5>以下程序的输出结果为( )。 #include "stdio.h" main() {int a; …

*【CodeForces - 195B】After Training (多解,模拟)

题干&#xff1a; After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from l…

pandas打印全部列_python——pandas练习题1-5

练习1-开始了解你的数据探索Chipotle快餐数据相应数据集&#xff1a;chipotle.tsvimport pandas as pd chipopd.read_csv("exercise_data/chipotle.tsv",sept) chipo.head(5)chipo.shape[0] #查看有多少行4622chipo.shape[1] #查看有多少列5chipo.columns #打印所…

【POJ - 2406】Power Strings (KMP,最小循环节)

题干&#xff1a; Given two strings a and b we define a*b to be their concatenation. For example, if a "abc" and b "def" then a*b "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative in…

python wmi mac变动_Python WMI参数反转

使用python的wmi模块创建vss快照&#xff0c;我发现除非将它们反向&#xff0c;否则这些参数将不起作用&#xff1a;importwmidefvss_create():shadow_copy_servicewmi.WMI(monikerwinmgmts:\\\\.\\root\\cimv2:Win32_ShadowCopy)resshadow_copy_service.Create(ClientAccessib…

【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)

题干&#xff1a; Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? Marge: OK. Homer: So I take…

python两次调用write连续写入的数据之间_两次调用文件的write 方法,以下选项中描述正确的是...

两次调用文件的write 方法&#xff0c;以下选项中描述正确的是答&#xff1a;连续写入的数据之间无分隔符中国大学MOOC: 斜弯曲、拉(压)弯曲组合变形的危险点都是单向应力状态。答&#xff1a;对急性宫外孕破裂或的最主要症状是答&#xff1a;突然一侧下腹部撕裂样疼痛Some thi…

拉格朗日差值 - 杜教板子

牛客网暑期ACM多校训练营(第一场) F Sum of Maximum 杜教板子&#xff1a; 证明https://blog.csdn.net/Lee_w_j__/article/details/81135539 #include <cstdio> #include <iostream> #include <vector> #include <cstring> #include <algorithm&…

python归并排序 分词_python实现归并排序,归并排序的详细分析

学习归并排序的过程是十分痛苦的。它并不常用&#xff0c;看起来时间复杂度好像是几种排序中最低的&#xff0c;比快排的时间复杂度还要低&#xff0c;但是它的执行速度不是最快的。很多朋友不理解时间复杂度低为什么运行速度不一定快&#xff0c;这个不清楚的伙伴可以看下我之…

CCFCSP 2018年9月 -- 部分题目

CCF201809 -- 第一题 &#xff1a;买菜 问题描述   在一条街上有n个卖菜的商店&#xff0c;按1至n的顺序排成一排&#xff0c;这些商店都卖一种蔬菜。   第一天&#xff0c;每个商店都自己定了一个正整数的价格。店主们希望自己的菜价和其他商店的一致&#xff0c;第二天…

servlet中显示mysql字段_Java Servlet:从数据库获取信息并在屏幕上显示它

创建它代表了表的每一个项目(行)一个JavaBean类。创建一个使用JDBC返回这些项目列表的DAO类。然后在servlet中&#xff0c;只需使用HttpServletRequest#setAttribute()将请求范围中的项目列表&#xff0c;使用RequestDispatcher#forward()将请求转发到JSP文件&#xff0c;并使用…

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

题干&#xff1a; Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered from 1 to n. Mike starts walking from his house located a…

sqlserver mysql分页_SQLServer与MySQL中分页查询sql语句示例

/***author blovedr*功能&#xff1a;SQLServer与MySQL中分页查询sql语句示例*日期&#xff1a; 2018年8月17日 10:58*注释&#xff1a; 学习数据库MySQL的点点记录&#xff0c; 谢谢网上各位大神分享经验与资料&#xff0c; 欢迎大神批评与交流。*/分页查询 2018.8.16 …