D. Yet Another Monster Killing Problem

冷暖自知 提交于 2020-01-17 05:24:14

http://codeforces.com/contest/1257/problem/D

D. Yet Another Monster Killing Problem
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
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

 

题意:给你n个monster  和他的能力值

    再给你m个hero   和他的能力值和耐力值

    当hero能力值大于等于monster时能打过   且能打过等同于他耐力值的monster

    一天上一个hero

    最少需要几天打完

#include <iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;


const int maxn=200010;

int a[maxn];
int power[maxn],en[maxn];
int hero[maxn];
void solve()
{
    int n;
    cin>>n;
    int maxm=-1,maxh=-1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        maxm=maxm>a[i]?maxm:a[i];    //maxm记录怪物的最大能力值
        hero[i]=0;
    }
    int k;
    cin>>k;
    for(int i=0;i<k;i++)
    {
        scanf("%d%d",&power[i],&en[i]);
        maxh=maxh>power[i]?maxh:power[i];       //maxh记录hero的最大能力值
        hero[en[i]-1]=hero[en[i]-1]>power[i]?hero[en[i]-1]:power[i];     //从第一天往后记录hero能处理的最大能力值
    }
    if(maxh<maxm)      //如果有怪物太强不能处理  就return
    {
        printf("-1\n");
        return ;
    }
    for(int i=n-2;i>=0;i--)
    {
        hero[i]=hero[i]>hero[i+1]?hero[i]:hero[i+1];       //hero能处理十天的一定能处理一天的
    }
    int i=0,ans=0,cons=-1,cur=-1;
    while(i<n)
    {
        cur=cur>a[i]?cur:a[i];              //cur记录当前要打的monster
        cons++;
        if(hero[cons]<cur)                  //如果能打过 就看cons+1天数的hero能打过否   如果打不过 那就再次从能力值一天的hero开始
        {
            ans++;
            cons=0;
            cur=a[i];                       //记录当前的最强monster
        }
        i++;                                //天数++
    }
    cout<<ans+1<<endl;                      //最后一天的没有算  ans++
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
/*
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
*/

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!