二分答案(Widespread )
二分答案其实是变相贪心,这周算是被这个虐了,怎么都想不到,比如这题,一直纠结在最大值的贪心上后面队友一指点,原来可以先减去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 3, 4, 1 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 )的更多相关文章
- CH Round #72树洞[二分答案 DFS&&BFS]
树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...
- [CF752E]Santa Claus and Tangerines(二分答案,dp)
题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...
- [NOIP2011] 聪明的质检员(二分答案)
题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...
- 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 ...
- {POJ}{3897}{Maze Stretching}{二分答案+BFS}
题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...
- Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)
貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的... 题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数. 二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第 ...
- CF 371C-Hamburgers[二分答案]
C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- NOIP2015跳石头[二分答案]
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选 择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石( ...
- 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)
4692: Beautiful Spacing Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 46 Solved: 21[Submit][Statu ...
随机推荐
- Grid 行和列
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <Co ...
- xshell 常用命令
一.grep 命令 (1)命令格式 grep [选项] pattern [file] (2)常用参数 参数 描述 -c 计算找到 '搜寻字符串'(即 pattern) 的次数 -i 忽略大小写的不同, ...
- 使用IDEA整合SSM框架
一.安装环境和开发工具 在整合Spring,SpringMVC 和 MyBatis 的过程中,很容易遇到一些小问题,因此记录下整合过程. 首先是安装环境和开发工具,如下: Window 7 Jdk 1 ...
- sql server 数据库复制实现数据同步常见问题(不定期更新)
sql server2008数据库复制实现数据同步常见问题 在原作者基础上追加 sql server2008数据库复制实现数据同步常见问题 23.发布 'xx' 的并发快照不可用,因为该快照尚未完全生 ...
- 并发-ConcurrentHashMap源码分析
ConcurrentHashMap 参考: http://www.cnblogs.com/chengxiao/p/6842045.html https://my.oschina.net/hosee/b ...
- Java中sleep()与wait()的区别
第一种解释: 功能差不多,都用来进行线程控制,他们最大本质的区别是:sleep()不释放同步锁,wait()释放同步缩. 还有用法的上的不同是:sleep(milliseconds)可以用 ...
- JNIjw05
ZC: 这个代码,没有真正的运行测试 1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw05.h" #in ...
- JAVA中的枚举使用总结
概念 在某些情况下,一个类的对象时有限且固定的,如季节类,它只有春夏秋冬4个对象这种实例有限且固定的类,在 Java 中被称为枚举类; 理解 类里面定义了固定数量的实例,类名如同命令空间 代码 pac ...
- nginx 相关资料
1.https://juejin.im/post/5a2600bdf265da432b4aaaba (nginx从入门到实践) 2.https://blog.csdn.net/hzsunshine/a ...
- TCP粘包处理 参考spserver
TCP粘包出现的原因就不在详细描述了.TCP粘包是在做TCP编程时经常会遇到的问题,网上相关的参考也不少,大都都是一个热心人士编写的, 若仅用于学习就算了,若用真正用于项目还有待考虑. 本文就简述一下 ...