二分答案(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 ...
随机推荐
- 给JSP应用提供JSTL支持(IntelliJ IDEA)
===========手动分割线===2018-12-26============================================= Maven项目直接添加如下依赖即可: <de ...
- Jquery简单的选项卡实现
概述 原来对jQuery用的不是很多,主要就是表单验证这些部分,最近想要更深入的学习jQuery和JavaScript编码,就找来了一些视频进行学习,然后就做了这个简单的选项卡示例.视频学习地址见最后 ...
- Dangling Javadoc comment
Javadoc主要用于对类和方法的注释.Javadoc没有@file和@date的注解.Javadoc has no @file or @date tags. You should be taggin ...
- CA认证_demo
CA认证,即电子认证服务 [1] ,是指为电子签名相关各方提供真实性.可靠性验证的活动. 证书颁发机构(CA, Certificate Authority)即颁发数字证书的机构.是负责发放和管理数字 ...
- C#与C++之间类型对应关系
//C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned c ...
- GridView右键菜单
一.添加右键菜单 1.在VS工具箱中的“菜单和工具栏”找到ContextMenuStrip控件,双击添加. 2.点击ContextMenuStrip右上方的小三角形,打开编辑项,可以添加菜单项.至于菜 ...
- Android的四种储存方式(SQLite、FileSystem、SDCardSystem、SharedPreferences)
主要记录一下安卓中几种常用的存储方式的用法. 一.SQLite 1.创建SQLiteOpenHelper对象(当然SQLiteOpenHelper是抽象类,不能直接创建): 2.通过上面创建的对象调用 ...
- Minimum Window Substring, 包含子串的最小窗口,双指针
问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...
- 自己用java实现飞鸽传书 1 - 实现socket通信
第一步: 建立服务端客户端,实现端到端通信.因为要传递文件,信号量较大,故使用TCP/IP协议. 服务端和客户端都要建立socket,而后通过socket进行通信.目前只实现服务端到客户端的单向通信. ...
- Element UI 框架搭建
Element UI 框架搭建 1.webpack 全局安装 npm install -g webpack 2.淘宝镜像cnpm安装 npm install -g cnpm --registry=ht ...