ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

D. Yet Another Monster Killing Problem

2020-07-07 23:38:06  阅读:311  来源: 互联网

标签:hero monster int Killing Problem include Yet day mod


You play a computer game. In this game, you lead a party of mm heroes, and you have to clear a dungeon with nn monsters. Each monster is characterized by its power aiai. Each hero is characterized by his power pipi and endurance sisi.

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 kk monsters, the hero fights with the monster k+1k+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 ii-th hero cannot defeat more than sisi 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 tt (1≤t≤1051≤t≤105) — the number of test cases. Then the test cases follow.

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

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109), where aiai is the power of the ii-th monster.

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

Then mm lines follow, each describing a hero. Each line contains two integers pipi and sisi (1≤pi≤1091≤pi≤109, 1≤si≤n1≤si≤n) — the power and the endurance of the ii-th hero.

It is guaranteed that the sum of n+mn+m over all test cases does not exceed 2⋅1052⋅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−1 if it is impossible).

Example input Copy
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 Copy
5
-1

 

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 998244353;
const int N = 1e6 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int add(int x, int y) {
    return ((x%mod)+(y%mod))%mod;
}
inline int sub(int x, int y) {
    x -= y;
    return x < 0 ? x += mod : x;
}
inline int mul(int x, int y) {
    return (1ll * (x %mod) * (y % mod))%mod;
}
inline int Inv(int x) {
    return qpow(x, mod - 2);
}
int mx[N],a[N],p[N],s[N];

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        rep(i, 1, n)
            a[i] = read();
        int m;
        cin >> m;
        rep(i, 1, m)
        {
            p[i] = read();
            s[i] = read();
            mx[s[i]] = max(mx[s[i]], p[i]);
        }
        dec(i, n, 0)
        {
            mx[i] = max(mx[i], mx[i + 1]);
        }
        int f=0,day = 0;
        for(int i=1;i<=n;)
        {
            if (a[i] > mx[1])
            {
                f = 1;
                cout << "-1" << endl;
                break;
            }
            int j = 0, maxn = 0;
            for (; j <= n; j++)
            {
                maxn = max(maxn, a[i + j]);
                if (mx[j + 1] < maxn)
                    break;
            }
            day++;
            i += j;
        }
        if (f == 0)
            cout << day << endl;
        rep(i, 0, n)
            mx[i] = 0;
    }
    return 0;
}

 

标签:hero,monster,int,Killing,Problem,include,Yet,day,mod
来源: https://www.cnblogs.com/dealer/p/13264232.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有