Educational Codeforces Round 125 (Rated for Div. 2)

比赛链接

Educational Codeforces Round 125 (Rated for Div. 2)

D. For Gamers. By Gamers.

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:

  • \(c_{i}\) - the cost of recruiting one unit of the \(i\)-th type;
  • \(d_{i}\) - the damage that one unit of the \(i\)-th type deals in a second;
  • \(h_{i}\) - the amount of health of one unit of the \(i\)-th type.
    Monocarp has to face \(m\) monsters. Every monster has two parameters:
  • \(D_{j}\) - the damage that the \(j\)-th monster deals in a second;
  • \(H_{j}\) - 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\left(1 \leq n \leq 3 \cdot 10^{5} ; 1 \leq C \leq 10^{6}\right)\) - 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 \(c_{i}, d_{i}\) and \(h_{i}\left(1 \leq c_{i} \leq C ; 1 \leq d_{i}, h_{i} \leq 10^{6}\right)\).
The next line contains a single integer \(m\left(1 \leq m \leq 3 \cdot 10^{5}\right)\) - the number of monsters that Monocarp has to face.
The \(j\)-th of the next \(m\) lines contains two integers \(D_{j}\) and \(H_{j}\left(1 \leq D_{j} \leq 10^{6} ; 1 \leq H_{j} \leq 10^{12}\right)\).

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\).

解题思路

二分,思维

设一个单元的血量为 \(h\),攻击为 \(d\),用了 \(x\) 个单位,一个怪兽的血量为 \(H\),攻击为 \(D\),则 \(\frac{H}{xd}< \frac{h}{D}\),即 \(x>\frac{HD}{hd}\),由于总花费 \(C\) 较小,可以预处理对于每个花费 \(i\) 可以造成的最大的 \(hd\),然后再二分答案即可

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: D. For Gamers. By Gamers.
// Contest: Codeforces - Educational Codeforces Round 125 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1657/problem/D
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}


int main()
{
    int n,C;
    cin>>n>>C;
    vector<LL> a(C+1,0);
    for(int i=1;i<=n;i++)
    {
    	LL c,d,h;
    	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)a[j]=max(a[j],j/i*a[i]);
    }
    for(int i=1;i<=C;i++)a[i]=max(a[i],a[i-1]);
    LL m,D,H;
    cin>>m;
    while(m--)
    {
    	cin>>D>>H;
    	D*=H;
    	int p=upper_bound(a.begin(),a.end(),D)-a.begin();
    	if(p==C+1)cout<<"-1 ";
    	else
    		cout<<p<<' ';
    }
    return 0;
}
posted @ 2022-03-23 13:42  zyy2001  阅读(43)  评论(0编辑  收藏  举报