题目描述

Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts 100^{500}100500 seconds, during which Monocarp attacks the dragon with a poisoned dagger. The ii -th attack is performed at the beginning of the a_iai​ -th second from the battle start. The dagger itself does not deal damage, but it applies a poison effect on the dragon, which deals 11 damage during each of the next kk seconds (starting with the same second when the dragon was stabbed by the dagger). However, if the dragon has already been poisoned, then the dagger updates the poison effect (i.e. cancels the current poison effect and applies a new one).

For example, suppose k = 4k=4 , and Monocarp stabs the dragon during the seconds 22 , 44 and 1010 . Then the poison effect is applied at the start of the 22 -nd second and deals 11 damage during the 22 -nd and 33 -rd seconds; then, at the beginning of the 44 -th second, the poison effect is reapplied, so it deals exactly 11 damage during the seconds 44 , 55 , 66 and 77 ; then, during the 1010 -th second, the poison effect is applied again, and it deals 11 damage during the seconds 1010 , 1111 , 1212 and 1313 . In total, the dragon receives 1010 damage.

Monocarp knows that the dragon has hh hit points, and if he deals at least hh damage to the dragon during the battle — he slays the dragon. Monocarp has not decided on the strength of the poison he will use during the battle, so he wants to find the minimum possible value of kk (the number of seconds the poison effect lasts) that is enough to deal at least hh damage to the dragon.

输入格式

The first line contains a single integer tt ( 1 \le t \le 10001≤t≤1000 ) — the number of test cases.

The first line of the test case contains two integers nn and hh ( 1 \le n \le 100; 1 \le h \le 10^{18}1≤n≤100;1≤h≤1018 ) — the number of Monocarp's attacks and the amount of damage that needs to be dealt.

The second line contains nn integers a_1a1​ , a_2a2​ , ..., a_nan​ ( 1 \le a_i \le 10^9; a_i < a_{i + 1}1≤ai​≤109;ai​<ai+1​ ), where a_iai​ is the second when the ii -th attack is performed.

输出格式

For each test case, print a single integer — the minimum value of the parameter kk , such that Monocarp will cause at least hh damage to the dragon.

题意翻译

Monocarp 要斩杀一头巨龙,巨龙的血量为 h。Monocarp 可以发动 n 次攻击,每次攻击发动于时间 ai,持续时间 k 秒,每持续 1 秒就会让巨龙的血量 -1。如果两个攻击的间隔小于 k,后一个攻击发动时会停止前一个攻击的效果。

Monocarp 想知道斩杀这头巨龙的 k 的最小值是多少。

输入输出样例

输入 #1

4
2 5
1 5
3 10
2 4 10
5 3
1 2 4 5 7
4 1000
3 25 64 1337

输出 #1

3
4
1
470

说明/提示

In the first example, for k=3k=3 , damage is dealt in seconds [1, 2, 3, 5, 6, 7][1,2,3,5,6,7] .

In the second example, for k=4k=4 , damage is dealt in seconds [2, 3, 4, 5, 6, 7, 10, 11, 12, 13][2,3,4,5,6,7,10,11,12,13] .

In the third example, for k=1k=1 , damage is dealt in seconds [1, 2, 4, 5, 7][1,2,4,5,7] .


只给一周时间做5道二分题,本菜鸟是真的会“谢”。

拿到这道题,我是毫无思绪,于是上了vjudge看了别人的题解,哟嘿,很开心第一个题解就很短,

然鹅,看了半天,为什么要left<=right呀,为什么返回的是right+1,为什么另一个题解写的返回left呀........

好晕啊,即使我照着别人的题解敲一遍,还是搞不懂,然后看了那么多题解,里面好像有涉及什么二分模板,好吧,这肯定是个我不知道的套路,于是我就去查,终于在一本算法书上找到了它。

two days later.......

我终于理解了二分模板,好吧,让我再次面对它吧,果然,题解一下子就变得和蔼可亲了呢

I got it!!!!!!


先来分析一下这个题: 这个思路借鉴了一位vjudge用户的题解

AC题解:

​#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
//用到long long 类型是因为防止在使用int时,mid=left+right 超过int最大数值
ll a[105],n,h;
int main(){int t;scanf("%d",&t);while(t--){scanf("%lld%lld",&n,&h);for(int i=1;i<=n;++i)scanf("%lld",&a[i]);ll left=1,right=1e18;//即使是2倍的right也不会超过long long的极限值 //二分核心步骤来了 while(left<right){ll mid=left+right>>1;//右移符>>1,相当于除以2(限于整数) ll ans=mid;for(int i=1;i<n;i++)ans+=min(mid,a[i+1]-a[i]);//ans的最终值即为通过k求出的总伤害 if(ans>=h)right=mid;elseleft=mid+1;}printf("%lld\n",left);}return 0;
}

我第一次接触那部分二分步骤模板的时候可是懵逼的很。

本题用到的二分模板:这是左右区间为闭区间的模板

//“寻找有序序列第一个满足某条件的元素的位置 ”问题的固定模板
//这个“某条件”一定是在序列中从左到右先不满足,然后满足的(否则把该条件取反即可)
//二分区间为左闭右闭的[left,right]
int solve(int left,int right){int mid;while(left<right){mid=(left+right)/2;if(条件成立){right=mid;}else{left=mid+1;}}return left;//循环结束的条件是left==right,则也可以返回right
}
/*如果想要寻找最后一个满足“条件C”的元素的位置,
可以先求第一个满足!C的位置,然后将该位置减1就行了 */

本题也可以用这个模板:左开右闭区间的模板

//解决“寻找有序序列第一个满足某条件的元素的位置”问题的固定模板
//这个“某条件”一定是在序列中从左到右先不满足,然后满足的(否则把该条件取反即可)
//二分区间为(left,right],left比最小取值小1,才能覆盖所有取值
int solve(int left,int right){int mid;while(left+1<right){mid=(left+right)/2;if(条件成立)right=mid;elseleft=mid;}return right;//也可以返回left+1,但不能返回left
} 

另外,解决“序列中是否存在满足某条件的元素”问题的模板 如下:

//这里以在一个递增序列里查找是否存在等于x的元素为例
int solve(int a[],int left ,int right,int x){int mid;while(left<=right){mid=(left+right)/2;if(a[mid]==x)return mid;else if(a[mid]>x)right=mid-1;elseleft=mid+1;}return -1;
}

这里只是给出了三个模板,我是从胡凡,曾磊写的《算法笔记》里看的。有兴趣的朋友可以看书中原文:我拍了图片放在这个链接里面了

poisoned dagger相关推荐

  1. Educational Codeforces Round 118 (Rated for Div. 2) C. Poisoned Dagger(二分或搜索)

    C. Poisoned Dagger 题意: Monocarp is playing yet another computer game. In this game, his character ha ...

  2. Educational Codeforces Round 118 (Rated for Div. 2)/C. Poisoned Dagger

    记录自己两个月前打CF以来第一个在时限内过掉的C题(好耶!) 简单二分 #include<bits/stdc++.h> using namespace std; #define ll lo ...

  3. 【CodeForces】Educational Codeforces Round 118 (Rated for Div. 2)【A-C】

    A. Long Comparison 思路 先判断字符长度 相等的字符长度判断填满0后判断字典序 AC代码 #pragma GCC optimize("Ofast") #pragm ...

  4. Educational Codeforces Round 118 (Rated for Div. 2) A-C

    D的难度跨度太大了,先不写了. A. Long Comparison 给出一个数字x1,后面添加p1个0,一个数字x2,后面添加p2个0.比较两个数字的大小,输出比较的情况. x和p都是10的6次方的 ...

  5. dagger android,dagger.android多模块项目实现

    本文适合有一定的Dagger2使用基础的同学 前两篇文章我们讲了两种多模块项目怎么使用Dagger2. 发现在每个Activity的onCreate中都需要调一个inject方法NewsCompone ...

  6. Android 依赖注入: Dagger 2 实例解说(一)

    本文原创,转载请注明出处:http://blog.csdn.net/zjbpku [Duplicated]   link to  Dagger on Android - Dagger2具体解释 关于D ...

  7. 用 Dagger 2 实现依赖注入

    原文地址:Dependency Injection with Dagger 2 原文作者:CodePath 译文出自:掘金翻译计划 译者: tanglie1993 校对者:mnikn, Zhiw 用 ...

  8. 开发日记-20190508 关键词 dagger Idea插件

    插件编写参考: https://www.jianshu.com/p/b0c7218678d8 IntelliJ IDEA编写插件入门(1):自动创建代码 https://github.com/Fran ...

  9. Dagger依赖注入注解的具体作用

    说真的,其实一开始说dagger并不依赖反射,其实我还是有点感兴趣的,因为通过注解来直接获取对应的对象本身其实也是很方便的一种东西,不过貌似对于性能还是有一定的影响的,不过,就个人而言,这种细枝末节的 ...

最新文章

  1. POJ 3982 序列 塔尔苏斯问题解决
  2. 与python相关的爬虫工具_python爬虫常用工具集合
  3. python3 一些常用的数学函数
  4. 如何启动php程序,如何第一次运行PHP程序?
  5. 十大经典排序算法4(Python版本)
  6. 插头DP题目泛做(为了对应WYD的课件)
  7. 嵌入式学习步骤及方法(精典)
  8. 当你的服务器被黑了,一定要看是不是犯了这 5 点错误
  9. 什么是WEBserver? 经常使用的WEBserver有哪些?
  10. Pandas速查手册中文版
  11. Tomcat内存溢出(OutOfMemoryError)
  12. 时速云与炎黄盈动强强联手,打造企业 IT 变革新未来
  13. MULTISIM安装下载
  14. [附源码]计算机毕业设计基于Springboot校园运动会管理系统
  15. oracle v session表,Oracle技术之V$SESSION_LONGOPS超过系统时间
  16. ip_forward 权限不够
  17. 异步FIFO的原理以及可综合的Verilog代码
  18. 安卓识别exfat_如何使安卓手机能够读写移动硬盘?
  19. CBI 图解机器人创业公司投资热点
  20. DataView的用法

热门文章

  1. linux开发技术栈
  2. 教你如何在centos7服务器中屏蔽掉那些高流量ip
  3. Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC
  4. html界面等待状态,html页面Loading效果实现:加载新页面前的等待过渡画面
  5. numpy.random.randn()与rand()的区别
  6. Linux性能分析工具详解
  7. 2022-7-8 Leetcode 904.水果成篮
  8. Android 语音播报 , 百度在线语音合成封装;
  9. windows下一些启动服务的命令
  10. PHP数据库分表查询