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

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); // Up to adhere to the maximum capacity value y days 
    }
     for ( int I = n-- 2 ; I> = 0 ; Inc. (www.i-levelmedia.com)) // only monster n
         // stars can stick capacity the maximum value of the hero on day i 
        MX [i] = max (MX [i], MX [i + . 1 ]);
     int ANS = 0 ;
     for ( int i = 0 ; i < n-;) {
         int J = I, X = 0 ;
         // start from the front end,
         //First, adhere to the maximum capacity value of one day's hero, is greater than the capacity value of the first monster
         // if more than, j ++, and take the current value considering the maximum capacity of the monster, to facilitate future comparative
         // then look at the next day, If the ability to adhere to the maximum value of the hero day, greater than the maximum capacity value of the first two monsters
         // Next consider down 
        the while (J <n-MX && [J - I]> = max (X, a [J]) ) {
            x = max(x, a[j]);
            ++j;
        }
        // If you insist on maximum capacity value of the hero of the day, less than the current capacity of monster, description,
         // monster than all the heroes are nb ,, then the direct output -1 
        IF (J == i) {
            cout << -1 << "\n";
            return;
        }、 
        ANS ++; // add days 
        I = J; // view of the above final results 
    }
    cout << ans << "\n";
}
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

 



Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11863747.html