Yet Another Monster Killing Problem (Codeforces 1257D)
Tags: Greedy, two pointers

You play a computer game. In this game, you lead a party of $$$m$$$ heroes, and you have to clear a dungeon with $$$n$$$ monsters. Each monster is characterized by its power $$$a_i$$$. Each hero is characterized by his power $$$p_i$$$ and endurance $$$s_i$$$.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 $$$k$$$ monsters, the hero fights with the monster $$$k + 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 $$$i$$$-th hero cannot defeat more than $$$s_i$$$ 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 $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Then the test cases follow.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of monsters in the dungeon.The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the power of the $$$i$$$-th monster.The third line contains one integer $$$m$$$ ($$$1 \le m \le 2 \cdot 10^5$$$) — the number of heroes in your party.Then $$$m$$$ lines follow, each describing a hero. Each line contains two integers $$$p_i$$$ and $$$s_i$$$ ($$$1 \le p_i \le 10^9$$$, $$$1 \le s_i \le n$$$) — the power and the endurance of the $$$i$$$-th hero.It is guaranteed that the sum of $$$n + m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

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).


Sample Inputs:
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1

Sample Outputs:
5
-1

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr, m) = (readInt(), readList(), readInt())
    power = [0] * (n + 1)
    for _ in range(m):
        (p, s) = readInts()
        power[s] = max(power[s], p)
    for i in range(n - 1, 0, -1):
        power[i] = max(power[i], power[i + 1])
    if max(arr) > power[1]:
        return -1
    (cnt, cp, ans) = (0, 0, 1)
    for i in range(n):
        cp = max(cp, arr[i])
        if cp <= power[cnt + 1]:
            cnt += 1
        else:
            ans += 1
            (cnt, cp) = (1, arr[i])
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr, m) = (readInt(), readList(), readInt())
    power = [0] * (n + 1)
    for _ in range(m):
        (p, s) = readInts()
        power[s] = max(power[s], p)
    for i in range(n - 1, 0, -1):
        power[i] = max(power[i], power[i + 1])
    if max(arr) > power[1]:
        return -1
    (cnt, cp, ans) = (0, 0, 1)
    for i in range(n):
        cp = max(cp, arr[i])
        if cp <= power[cnt + 1]:
            cnt += 1
        else:
            ans += 1
            (cnt, cp) = (1, arr[i])
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = nl()
    m = ni()
    g = [0] * (n + 2)
    for _ in range(m):
        (s, p) = nm()
        g[p] = max(g[p], s)
    for i in range(n, -1, -1):
        g[i] = max(g[i], g[i + 1])
    if max(a) > max(g):
        print(-1)
        return
    ans = idx = 0
    while idx < n:
        cm = a[idx]
        ct = 1
        while idx + ct < n and max(cm, a[idx + ct]) <= g[ct + 1]:
            cm = max(cm, a[idx + ct])
            ct += 1
        ans += 1
        idx += ct
    print(ans)
    return
T = ni()
for _ in range(T):
    solve()