首页 > 其他 > 详细

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

时间:2019-11-15 09:20:00      阅读:217      评论:0      收藏:0      [点我收藏+]

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 sisimonsters 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 (1t1051≤t≤105) — the number of test cases. Then the test cases follow.

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

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

The third line contains one integer mm (1m21051≤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 (1pi1091≤pi≤109, 1sin1≤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 21052⋅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 <bits/stdc++.h>
using namespace std;
void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    vector<int> mx(n);
    int m;
    cin >> m;
    for (int i = 0; i < m; ++i) {
        int x, y;
        cin >> x >> y;
        mx[y - 1] = max(mx[y - 1], x);//最多坚持y天的最大能力值
    }
    for (int i = n - 2; i >= 0; --i)//n只怪兽
        //得出可以坚持i天的英雄的最大能力值
        mx[i] = max(mx[i], mx[i + 1]);
    int ans = 0;
    for (int i = 0; i < n; ) {
        int j = i, x = 0;
        //从前端开始,
        //首先坚持1天的英雄的最大能力值,是否大于第一个怪兽的能力值
        //如果大于,j++,并取当前考虑过的怪兽的最大能力值,方便以后比较
        //然后看第二天,如果能坚持两天的英雄的最大能力值,大于前两个怪兽的最大能力值
        //接着往下考虑
        while (j < n && mx[j - i] >= max(x, a[j])) {
            x = max(x, a[j]);
            ++j;
        }
        //如果坚持一天的英雄的最大能力值,小于当前怪兽的能力,说明,
        //这个怪兽比所有英雄都nb,,那么直接输出-1
        if (j == i) {
            cout << -1 << "\n";
            return;
        }、 
        ++ans;//加天数
        i = j;//上面考虑的最终结果
    }
    cout << ans << "\n";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

 



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

原文:https://www.cnblogs.com/QingyuYYYYY/p/11863747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!