二分答案(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 ...
随机推荐
- Spring_使用外部属性文件
beans-properties.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...
- Spring 模块
- Spring Boot的核心
1.1.1. 入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序 ...
- Android开发——View的生命周期总结
0.前言 今天看到一个概念是View的生命周期,有点懵逼,听说过Activity的生命周期,Fragment的生命周期,对View的生命周期好像没什么概念啊.难道layout.draw这些也算是生命周 ...
- jsonp跨域传过来的数据格式
var uri1 = 'https://api.github.com?callback=JSON_CALLBACK'; /**/JSON_CALLBACK({ "meta": { ...
- jmeter的学习(配置环境)
jmeter是一款开源工具.解压即可使用 前提要安装配置jdk.验证jdk是否安装配置成功.运行cmd输入“ java -version” 如上图所示.jdk安装配置成功:64位版本1.8 现在即可进 ...
- yii2:doajax(post)会报500错误
yii2:doajax(post)会报500错误:这是因为yii2开启了防御csrf的攻击机制,可去先去掉,在控制器里去掉:public $enableCsrfValidation = false , ...
- URL的编码
+ URL 中+号表示空格 %2B 空格 URL中的空格可以用+号或者编码 %20 / 分隔目录和子目录 %2F ? 分隔实际的 URL 和参数 %3F % 指定特殊字符 %25 # 表示书签 %23 ...
- 慕课网:4-2—— 使用DB facade实现CURD (09:11)
public function test1() { //新增数据: /* $bool=DB::insert('insert into student(name,age) VALUES (?,?)', ...
- scrapy的简单使用以及相关设置属性的介绍
0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...