Educational Codeforces Round 125 (Rated for Div. 2) D. For Gamers. By Gamers.

D. For Gamers. By Gamers.
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has C coins to spend on his squad.

Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with C coins.

There are n types of units. Every unit type has three parameters:

ci — the cost of recruiting one unit of the i-th type;
di — the damage that one unit of the i-th type deals in a second;
hi — the amount of health of one unit of the i-th type.
Monocarp has to face m monsters. Every monster has two parameters:

Dj — the damage that the j-th monster deals in a second;
Hj — the amount of health the j-th monster has.
Monocarp has to fight only the j-th monster during the j-th battle. He wants all his recruited units to stay alive. Both Monocarp’s squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.

For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than C, then report that it’s impossible to kill that monster.

Input
The first line contains two integers n and C (1≤n≤3⋅105; 1≤C≤106) — the number of types of units and the amount of coins Monocarp has before each battle.

The i-th of the next n lines contains three integers ci,di and hi (1≤ci≤C; 1≤di,hi≤106).

The next line contains a single integer m (1≤m≤3⋅105) — the number of monsters that Monocarp has to face.

The j-th of the next m lines contains two integers Dj and Hj (1≤Dj≤106; 1≤Hj≤1012).

Output
Print m integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than C, then print −1.

Examples
input

3 10
3 4 6
5 5 5
10 3 4
3
8 3
5 4
10 15

outputCopy

5 3 -1 

inputCopy

5 15
14 10 3
9 2 2
10 4 3
7 3 5
4 3 1
6
11 2
1 1
4 7
2 1
1 14
3 3

outputCopy

14 4 14 4 7 7 

inputCopy

5 13
13 1 9
6 4 5
12 18 4
9 13 2
5 4 5
2
16 3
6 2

outputCopy

12 5 

Note
Consider the first monster of the first example.

Monocarp can’t recruit one unit of the first type, because it will take both him and the monster 0.75 seconds to kill each other. He can recruit two units for the cost of 6 coins and kill the monster in 0.375 second.

Monocarp can recruit one unit of the second type, because he kills the monster in 0.6 seconds, and the monster kills him in 0.625 seconds. The unit is faster. Thus, 5 coins is enough.

Monocarp will need at least three units of the third type to kill the first monster, that will cost 30 coins.

Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type.

解题思路

用硬币数C作为数组,对于每个硬币取最大性价比 (d*h)
然后用二分查找需要达到怪兽当前性价比所需的最小硬币数量(因为要保证大于所以要求后继)

AC代码

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, C, m;
const int N = 1e6 + 5;
ll a[N], f[N];
int main() {
	cin>>n>>C;
	ll D, H, d, h, c;
	for (int i=1; i<=n; ++i) {
		cin>>c>>d>>h;
		a[c] = max(a[c], d * h);
	}
	for (int i=1; i<=C; ++i) {
		if (a[i]==0) continue;
		for (int j=i; j<=C; j+=i) {
			f[j] = max(f[j], j / i * a[i]);
		}
	}
	for (int i=1; i<=C; ++i)
		f[i] = max(f[i], f[i - 1]);//如果硬币较小的性价比更大,那么当前硬币的性价比就可替换为较小的那个
	cin>>m;
	for (int i=1; i<=m; ++i) {
		cin>>D>>H;
		ll cur = D * H;
		int pos = upper_bound(f + 1, f + 1 + C, cur) - f;
		if (pos <= C)
			printf("%lld ", pos);
		else
			printf("-1 ");
	}
	puts("");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nebula_xuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值