Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem 贪心

D. Yet Another Monster Killing Problem

You play a computer game. In this game, you lead a party of 𝑚 heroes, and you have to clear a dungeon with 𝑛 monsters. Each monster is characterized by its power 𝑎𝑖. Each hero is characterized by his power 𝑝𝑖 and endurance 𝑠𝑖.

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 𝑘 monsters, the hero fights with the monster 𝑘+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 𝑖-th hero cannot defeat more than 𝑠𝑖 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 𝑡 (1≤𝑡≤105) — the number of test cases. Then the test cases follow.

The first line of each test case contains one integer 𝑛 (1≤𝑛≤2⋅105) — the number of monsters in the dungeon.

The second line contains 𝑛 integers 𝑎1, 𝑎2, ..., 𝑎𝑛 (1≤𝑎𝑖≤109), where 𝑎𝑖 is the power of the 𝑖-th monster.

The third line contains one integer 𝑚 (1≤𝑚≤2⋅105) — the number of heroes in your party.

Then 𝑚 lines follow, each describing a hero. Each line contains two integers 𝑝𝑖 and 𝑠𝑖 (1≤𝑝𝑖≤109, 1≤𝑠𝑖≤𝑛) — the power and the endurance of the 𝑖-th hero.

It is guaranteed that the sum of 𝑛+𝑚 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

input
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
output
5
-1

题意

有n个怪兽 ,每个怪兽都有能力值a[i]。

然后现在你有m个英雄,每个英雄也有能力值p[i],每个英雄还有一个s[i],表示这个英雄一天最多能消灭多少个怪兽

现在你必须一个接一个的消灭怪兽,不能改变顺序,然后问你最少多少天,能够消灭所有的怪兽

题解

我们维护一个数组d[i],表示至少坚持i天的英雄的能力最大是多少。

然后我们对于每一天,进行贪心选择最多的即可。我们优先看坚持一天的,能否大于最大的,然后看坚持两天的,这样一直看下去即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int power[maxn],en[maxn],mons[maxn],hero[maxn];
void solve(){
	int n,mxm=-1,mxh=-1,m,ans=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&mons[i]);
		mxm=max(mxm,mons[i]);
		hero[i]=0;
	}
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		scanf("%d%d",&power[i],&en[i]);
		mxh=max(mxh,power[i]);
		hero[en[i]-1]=max(hero[en[i]-1],power[i]);
	}
	if(mxh<mxm){
		puts("-1");
		return;
	}
	for(int i=n-2;i>=0;i--){
		hero[i]=max(hero[i],hero[i+1]);
	}
	int day=0,cur=-1,cons=-1;
	while(day<n){
		cur=max(cur,mons[day]);
		cons++;
		if(hero[cons]<cur){
			ans++;
			cons=0;
			cur=mons[day];
		}
		day++;
	}
	ans++;
	cout<<ans<<endl;
}
int main(){
	int t;scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
posted @ 2019-11-14 19:33  qscqesze  阅读(717)  评论(0编辑  收藏  举报