D. Yet Another Monster Killing Problem
You play a computer game. In this game, you lead a party of m heroes, and you have to clear a dungeon with n monsters. Each monster is characterized by its power ai. Each hero is characterized by his power pi and endurance si.
The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.
When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated k monsters, the hero fights with the monster k+1). When the hero fights the monster, there are two possible outcomes:
if the monster’s power is strictly greater than the hero’s power, the hero retreats from the dungeon. The current day ends;
otherwise, the monster is defeated.
After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the i-th hero cannot defeat more than si monsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.
Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don’t fight the monsters at all. Each hero can be used arbitrary number of times.
Input
The first line contains one integer t (1≤t≤105) — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer n (1≤n≤2⋅105) — the number of monsters in the dungeon.
The second line contains n integers a1, a2, …, an (1≤ai≤109), where ai is the power of the i-th monster.
The third line contains one integer m (1≤m≤2⋅105) — the number of heroes in your party.
Then m lines follow, each describing a hero. Each line contains two integers pi and si (1≤pi≤109, 1≤si≤n) — the power and the endurance of the i-th hero.
It is guaranteed that the sum of n+m over all test cases does not exceed 2⋅105.
Output
For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or −1 if it is impossible).
贪心+二分;
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=200100;
const LL mod=1e9+7;
int a[N];
int sta[N];
struct Node{
int p,s;
}hero[N];
int p1[N],s1[N];
bool cmp(Node p,Node q){
if(p.p==q.p) return p.s>q.s;
return p.p<q.p;
}
int ok;
int ans;
bool judge(int mx,int day){
int pos=lower_bound(p1+1,p1+ans+1,mx)-p1;
if(pos>=ans+1) ok=0;
if(s1[pos]>=day) return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int m;
cin>>m;
for(int i=1;i<=m;i++) cin>>hero[i].p>>hero[i].s;
sort(hero+1,hero+m+1,cmp);
ans=0;
for(int i=1;i<=m;i++){//单调栈
while(ans&&hero[i].s>=hero[sta[ans]].s) ans--;
sta[++ans]=i;
}
for(int i=1;i<=ans;i++){
p1[i]=hero[sta[i]].p;
s1[i]=hero[sta[i]].s;
}
ok=1;
int cnt=0;
int day=1;
int mx=0;
for(int i=1;i<=n;i++){
if(ok==0) break;
mx=a[i];
while(judge(mx,day)){
i++;
day++;
mx=max(mx,a[i]);
if(ok==0||i>n) break;
}
day=1;
i--;
cnt++;
}
if(ok) cout<<cnt<<endl;
else cout<<-1<<endl;
}
return 0;
}

本文探讨了一个计算机游戏中的策略问题,玩家需要领导英雄团队清除地牢中的怪物。文章详细介绍了游戏规则,包括英雄的攻击力和耐力,以及如何通过最少的天数击败所有怪物。使用了贪心算法和二分搜索来解决这个问题。
1301

被折叠的 条评论
为什么被折叠?



