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

Posted qscqesze

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem 贪心的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27