当前位置: 首页 > 工具软件 > Monster > 使用案例 >

Yet Another Monster Killing Problem

支华池
2023-12-01

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

Example
inputCopy
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
outputCopy
5
-1

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define maxn 1000010
typedef long long ll;
struct Tree{
	int l,r,mx;
}tree[maxn<<2];
struct Hero{
	int p,s;
}h[maxn];
int cnt=0,a[maxn],q1[maxn],q2[maxn];
bool cmp(Hero h1,Hero h2){
	if(h1.s==h2.s) return h1.p>h2.p;
	return h1.s>h2.s;
}
void pushup(int k){
	tree[k].mx=max(tree[k<<1].mx,tree[k<<1|1].mx);
}
void build(int l,int r,int k){
	tree[k].l=l; tree[k].r=r;
	if(l==r){
		tree[k].mx=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,k<<1);
	build(mid+1,r,k<<1|1);
	pushup(k);
}
int query(int l,int r,int k){
	if(l<=tree[k].l && tree[k].r<=r) return tree[k].mx;
	int mid=(tree[k].l+tree[k].r)>>1,ans=0;
	if(l<=mid) ans=max(ans,query(l,r,k<<1));
	if(r> mid) ans=max(ans,query(l,r,k<<1|1));
	return ans;
}
bool check(int l,int r){
	int mx=query(l,r,1);
	int po=lower_bound(q1+1,q1+cnt+1,mx)-q1;
	if(q2[po]>=(r-l+1)) return true;
	return false;
}
int main(){
	int t; scanf("%d",&t);
	while(t--){
		cnt=0;
		int n; scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		build(1,n,1);
		int m; scanf("%d",&m);
		bool flag=false;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&h[i].p,&h[i].s);
			if(h[i].p>=tree[1].mx) flag=true;
		} 
		if(!flag) puts("-1");
		else{
			sort(h+1,h+m+1,cmp);
			int p=1,days=0;
			for(int i=1;i<=m;i++){
				if(h[i].p<=q1[cnt] && h[i].s<=q2[cnt]) continue;
				cnt++; q1[cnt]=h[i].p; q2[cnt]=h[i].s;
			}
			while(p<=n){
				int l=p,r=n,pos=0;
				while(l<=r){
					int mid=(l+r)>>1;
					if(check(p,mid)){
						pos=mid;
						l=mid+1;
					}
					else r=mid-1;
				}
				p=pos+1;
				days++;
			}
			printf("%d\n",days);
		}
	}
	return 0;
} 
 类似资料:

相关阅读

相关文章

相关问答