二分答案其实是变相贪心,这周算是被这个虐了,怎么都想不到,比如这题,一直纠结在最大值的贪心上后面队友一指点,原来可以先减去x*b,然后a-b随机分配就好了,

仔细一想没错呀,每次攻击必然受到x*b次伤害而剩下的x个a-b就可以随机分配给每个怪物,注意是成对分而不能求和。说下二分答案吧

二分前提

1.答案区间上下限确定,即最终答案在哪个范围是容易知道的。

2.检验某值是否可行是个简单活,即给你个值,你能很容易的判断是不是符合题目要求。

3.可行解满足区间单调性,即若x是可行解,则在答案区间内x+1(也可能是x-1)也可行。

二分转换如下,但是检验是否成立就需要对题目的分析了。

1.最小值最大化

int l = min_ans, r = max_ans;
while (l < r) {
int mid = (l + r + ) / ; //+1避免 r == l + 1 时mid一直等于l,从而死循环
if (ok(mid)) //符合条件返回True
l = mid;
else
r = mid - ;
}

2.最大值最小化

 int l = min_ans, r = max_ans;
while (l < r) {
int mid = (l + r) / ;
if (ok(mid)) //符合条件返回True
r = mid;
else
l = mid + ;
}

题目:

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is hi at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

  • Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A>B holds.

At least how many explosions do you need to cause in order to vanish all the monsters?

Constraints

  • All input values are integers.
  • 1≤N≤105
  • 1≤B<A≤109
  • 1≤hi≤109

Input

Input is given from Standard Input in the following format:

N A B
h1
h2
:
hN

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

You can vanish all the monsters in two explosion, as follows:

  • First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 341 and −1, respectively, and the last monster vanishes.
  • Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0−1 and −2, respectively, and all the monsters are now vanished.

Sample Input 2

2 10 4
20
20

Sample Output 2

4

You need to cause two explosions centered at each monster, for a total of four.

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define eps 1e-7
#define maxi 100005
long long h[maxi];
long long hi[maxi];
long long n,a,b;
long long in=;
bool C(long long x){
long long t=x;
for(long long i=;i<n;i++)
hi[i]=h[i];
for(long long i=;i<n;i++){
hi[i]-=x*b;
if(hi[i]<=) continue;
long long y;
if(hi[i]%(a-b)==)
y=hi[i]/(a-b);
else
y=hi[i]/(a-b)+;
t-=y;
}
if(t>=) return true;
return false;
}
void solve(){
sort(h,h+n);
long long lb=,ub=in;
while(ub-lb>){
long long mid=(lb+ub)/;
//cout<<mid<<" "<<C(mid)<<" "<<ub<<endl;
if(C(mid)) ub=mid;
else lb=mid;
//cout<<lb<<" "<<ub<<endl;
}
cout<<ub<<endl;
}
int main()
{
scanf("%d%d%d",&n,&a,&b);
for(int i=;i<n;i++) scanf("%d",&h[i]);
solve();
return ;
}
												

二分答案(Widespread )的更多相关文章

  1. CH Round #72树洞[二分答案 DFS&&BFS]

    树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...

  2. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

  3. [NOIP2011] 聪明的质检员(二分答案)

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...

  4. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  5. {POJ}{3897}{Maze Stretching}{二分答案+BFS}

    题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...

  6. Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)

    貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的... 题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数. 二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第 ...

  7. CF 371C-Hamburgers[二分答案]

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. NOIP2015跳石头[二分答案]

    题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选 择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石( ...

  9. 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)

    4692: Beautiful Spacing Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 46  Solved: 21[Submit][Statu ...

随机推荐

  1. ijkplayer实现IMediaDataSource

    由于ijkplayer不能识别android.resource类型的资源在播放raw中的文件的时候用IjkMediaPlayer不能正常播放,实现IMediaDataSource为IjkMediaPl ...

  2. 一些好用的Linux命令工具

    刚好看到一个系列的博文,讲的就是那些平时很少被人用到但是很实用的命令,选一些觉得自己应该比较常用到的出来写个笔记. 这些命令有些不是系统命令,需要自行下载安装.Debian下安装:sudo apt-g ...

  3. caffe python lmdb读写

    caffe中可以采取lmdb健值数据库的方式向网络中输入数据. 所以操作lmdb就围绕"键-值"的方式访问数据库就好了. Write 我们可以采用cv2来读入自己的图像数据,采用d ...

  4. Eclipse 添加JSP模板

    0.环境 Eclipse IDE for Java EE Developers (4.3.2) win8.1系统 1.原因 Eclipse自带新建JSP为: <%@ page language= ...

  5. codeforces781A Andryusha and Colored Balloons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. Shell中find中的atime、ctime、mtime的区别

    Shell中find中的atime.ctime.mtime的区别 find用法: -atime n File was last accessed n*24 hours ago. 访问(读取文件或执行文 ...

  7. Pandas字符串和文本数据

    在本章中,我们将使用基本系列/索引来讨论字符串操作.在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame). Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作. ...

  8. TUNING FOR ALL FLASH DEPLOYMENTS

    Ceph Tuning and Best Practices for All Flash Intel® Xeon® ServersLast updated: January 2017 TABLE OF ...

  9. 真相大白:为什么Entity Framework不能进行跨数据库查询(附解决方法)

    在上篇随笔中,我们发现Entity Framework在构建SQL语句时,将ToTable("CNBlogsTex.dbo.blog_PostBody")中的"CNBlo ...

  10. xml、json的序列化与反序列化

    xml数据 : XmlSerializer.Serialize   与  XmlSerializer.Deserialize,使用起来稍有些复杂,需要对 “实体模型” 的“对应属性”  进行  节点特 ...