开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第26天,点击查看活动详情 B. Incinerate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
To destroy humanity, The Monster Association sent nn monsters to Earth's surface. The ii-th monster has health hihi and power pipi.
With his last resort attack, True Spiral Incineration Cannon, Genos can deal kk damage to all monsters alive. In other words, Genos can reduce the health of all monsters by kk (if k>0k>0) with a single attack.
However, after every attack Genos makes, the monsters advance. With their combined efforts, they reduce Genos' attack damage by the power of the ††weakest monster ‡‡alive. In other words, the minimum pipi among all currently living monsters is subtracted from the value of kk after each attack.
††The Weakest monster is the one with the least power.
‡‡A monster is alive if its health is strictly greater than 00.
Will Genos be successful in killing all the monsters?
Input
The first line of the input contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers, nn and kk (1≤n,k≤1051≤n,k≤105) — the number of monsters and Genos' initial attack damage. Then two lines follow, each containing nn integers describing the arrays hh and pp (1≤hi,pi≤1091≤hi,pi≤109).
It's guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print the answer — "YES" (without quotes) if Genos could kill all monsters and "NO" otherwise.
Example
input
Copy
3
6 7
18 5 13 9 10 1
2 7 2 1 2 6
3 4
5 5 5
4 4 4
3 2
2 1 3
1 1 1
output
Copy
YES
NO
YES
Note
In the first example, after Genos' first attack, hh and kk will update to:
- h:[11,0,6,2,3,0]h:[11,0,6,2,3,0]
- k:7−1=6k:7−1=6
After second attack:
- h:[5,0,0,0,0,0]h:[5,0,0,0,0,0]
- k:6−2=4k:6−2=4
After third attack:
- h:[1,0,0,0,0,0]h:[1,0,0,0,0,0]
- k:4−2=2k:4−2=2
After fourth attack:
- h:[0,0,0,0,0,0]h:[0,0,0,0,0,0]
As Genos could kill all monsters, the answer is YES.
分析
这题暴力有可能会卡,所以这题其实就是用一个堆去维护一个temp,如果h小于temp就pop,否则就将k减p,然后temp+上k,最后堆空就yes,否则再看队内所有元素是不是都小于等于temp,是就输出yes,否则输出no。
代码
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#define int long long
#define PII pair<int,int>
using namespace std;
const int N=100010;
struct pp{
int h,p;
}a[N];
bool cmp(pp x,pp y){
return x.p<y.p;
}
signed main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
priority_queue<PII,vector<PII>,greater<PII>> heap;
for(int i=1;i<=n;i++){
cin>>a[i].h;
}
for(int i=1;i<=n;i++){
cin>>a[i].p;
}
int temp=k;
for(int i=1;i<=n;i++){
heap.push({a[i].p,a[i].h});
}
while(heap.size() && k>0){
int x=heap.top().first,y=heap.top().second;
if(y-temp<=0){
heap.pop();
}
else{
k-=x;
y-=temp;
temp+=k;
}
}
if(heap.empty()){
puts("YES");
continue;
}
else{
while(heap.size()){
if(heap.top().second<=temp){
heap.pop();
}
else{
break;
}
}
if(heap.empty()){
puts("YES");
}
else{
puts("NO");
}
}
}