<span>AtCoder Beginner Contest 063 - D - Widespread (二分答案)</span>

AtCoder Beginner Contest 063 - D - Widespread (二分答案)

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter NN monsters. Each monster has a parameter called health, and the health of the ii-th monster is hihi at the moment of encounter. A monster will vanish immediately when its health drops to 00 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 AA, and the health of each of the other monsters will decrease by BB. Here, AA and BB are predetermined parameters, and A>BA>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≤1051≤N≤105
  • 1≤B<A≤1091≤B<A≤109
  • 1≤hi≤109

Input

Input is given from Standard Input in the following format:

NN AA BB
h1h1
h2h2
::
hNhN

Output

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

Sample Input 1 Copy

Copy

4 5 3
8
7
4
2

Sample Output 1 Copy

Copy

2

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

  • First, cause an explosion centered at the monster with 88 health. The healths of the four monsters become 33, 44, 11 and −1−1, respectively, and the last monster vanishes.
  • Second, cause an explosion centered at the monster with 44 health remaining. The healths of the three remaining monsters become 00, −1−1 and −2−2, respectively, and all the monsters are now vanished.

Sample Input 2 Copy

Copy

2 10 4
20
20

Sample Output 2 Copy

Copy

4

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

题意:

你面对n个怪兽,第i个怪兽有h[i]健康值,你每一次攻击可以选择一个还活着的怪兽,然后使其健康值下降A,其他的怪兽的健康值下降B。当一个怪兽的健康值小于等于0时,这个怪兽就会消失。

现在问你最少需要多少次攻击可以使所有的怪兽都消失。

思路:

设函数F(x)代表攻击x次,最多可以消灭怪兽的数量。

容易知道该函数是一个单调不下降的函数。

于是我们可以二分答案ans,二分过程中去检测攻击ans次是否可以消灭所有怪兽。

距离检测过程为:

将所有怪兽健康值h[i]减去 ans*b, 减去后的h[i]仍大于0的 再减去\(cnt_i= \lceil \frac{h[i]}{(A-B)} \rceil\)

(注意h[i] 减去了ans*b后的)

然后返回\(ans>=\sum_{i=1}^n cnt_i\) 即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
ll h[maxn];
ll b, a;
bool check(ll x)
{
	ll res = x;
	repd(i, 1, n)
	{
		ll temp = h[i] - x * b;
		if (temp > 0)
		{
			ll need = temp / (a - b);
			if (temp % (a - b) != 0)
			{
				need++;
			}
			res -= need;
			if (res < 0)
			{
				break;
			}
		}
	}
	return res >= 0;
}
int main()
{
	//freopen("D:\\code\\text\\input.txt","r",stdin);
	//freopen("D:\\code\\text\\output.txt","w",stdout);
	n = readint();
	a = readll();
	b = readll();
	repd(i, 1, n)
	{
		h[i] = readll();
	}
	ll l = 1ll;
	ll r = inf;
	ll mid;
	ll ans;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (check(mid))
		{
			ans = mid;
			r = mid - 1;
		} else
		{
			l = mid + 1;
		}
	}
	printf("%lld\n", ans );
	return 0;
}



全部评论

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务