二分答案其实是变相贪心,这周算是被这个虐了,怎么都想不到,比如这题,一直纠结在最大值的贪心上后面队友一指点,原来可以先减去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. make cmake catkin_make

    在Linux下进行C语言编程,必然要采用GNU GCC来编译C源代码生成可执行程序. 一.GCC快速入门 Gcc指令的一般格式为:Gcc [选项] 要编译的文件 [选项] [目标文件] 其中,目标文件 ...

  2. onerror事件

    onerror 事件会在文档或图像加载过程中发生错误时被触发. 案例: <img onerror="this.onerror=null;this.src='/images/common ...

  3. kali 2016:mount ntfs 分区只读 --Falling back to read-only mount because the NTFS partition is in an unsafe state.

    mount ntfs 分区 mount /dev/sdb1 /mnt/d 提示: The disk contains an unclean file system (0, 0).Metadata ke ...

  4. 20145219 《Java程序设计》实验四 Android开发基础设计实验报告

    20145219 <Java程序设计>实验四 Android开发基础设计实验报告 实验内容 安装Andriod Studio并配置软件 使用Andriod Studio软件实现Hello ...

  5. 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...

  6. Oracle中对现有表增加列

    altertable Tablename add(column1 varchar2(20),column2 number(7,2)...) --Oracle中修改列名不可以,但是可以删除列,增加列 a ...

  7. php上传文件后无法移动到指定目录的解决

    从浏览器访问而触发PHP脚本运行的用户是 apache 用户 无法移动文件的原因主要是目标目录没有写入权限 1.将目标目录权限设置为 777 #chmod 777 tar_dir 2.将目标目录用户和 ...

  8. LAMP服务器的搭建

    LAMP是一组构建Web应用平台的开源软件解决方案,它是一个开源套件组合.其中L:linux,A :Apache HTTP服务器,M : MySQL或MariaDB,P : perl或Python.这 ...

  9. tomcat绿色版——运行一闪而过的解决方法

    首先配置好jdk的环境变量 %JAVA_HOME%\bin;注意:一定是英文状态下的分号结尾 %TOMCAT%\bin;注意:一定是英文状态下的分号结尾 service.bat install ser ...

  10. Android 相关重难点知识整理

    [原文] 集合 对 HashMap 进行排序: HashMap 本身无序,但其子类 LinkedHashMap 使用链表结构,实现了有序.通过 HashMap#entrySet() 方法可以将 Map ...