Standard Free2play (Codeforces 1238C)
Tags: Greedy, dp

You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height $$$h$$$, and there is a moving platform on each height $$$x$$$ from $$$1$$$ to $$$h$$$.Each platform is either hidden inside the cliff or moved out. At first, there are $$$n$$$ moved out platforms on heights $$$p_1, p_2, \dots, p_n$$$. The platform on height $$$h$$$ is moved out (and the character is initially standing there).If you character is standing on some moved out platform on height $$$x$$$, then he can pull a special lever, which switches the state of two platforms: on height $$$x$$$ and $$$x - 1$$$. In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another.Your character is quite fragile, so it can safely fall from the height no more than $$$2$$$. In other words falling from the platform $$$x$$$ to platform $$$x - 2$$$ is okay, but falling from $$$x$$$ to $$$x - 3$$$ (or lower) is certain death. Sometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height $$$h$$$, which is unaffected by the crystals). After being used, the crystal disappears.What is the minimum number of magic crystal you need to buy to safely land on the $$$0$$$ ground level?

Input: The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query contains two lines and is independent of all other queries.The first line of each query contains two integers $$$h$$$ and $$$n$$$ ($$$1 \le h \le 10^9$$$, $$$1 \le n \le \min(h, 2 \cdot 10^5)$$$) — the height of the cliff and the number of moved out platforms.The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$h = p_1 > p_2 > \dots > p_n \ge 1$$$) — the corresponding moved out platforms in the descending order of their heights.The sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.

Output: For each query print one integer — the minimum number of magic crystals you have to spend to safely come down on the ground level (with height $$$0$$$).


Sample Inputs:
4
3 2
3 1
8 6
8 7 6 5 3 2
9 6
9 8 5 4 3 1
1 1
1

Sample Outputs:
0
1
2
0

Solution (Python):
import sys
q = int(input())
for _ in range(q):
    (h, n) = map(int, input().split())
    platforms = list(map(int, input().split())) + [0]
    i = 1
    res = 0
    while i < n:
        if platforms[i] - 1 == platforms[i + 1]:
            i += 2
            continue
        res += 1 if platforms[i] > 1 else 0
        i += 1
    print(res)

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

def solve(h, n, P):
    prev2 = 0
    if n < 2:
        print(prev2)
        return
    prev1 = 0 if P[n - 1] <= 1 else 1
    ans = prev1
    for i in range(n - 3, -1, -1):
        ans = prev2 if P[i + 2] + 1 == P[i + 1] else prev1 + 1
        prev2 = prev1
        prev1 = ans
    print(ans)
for _ in range(int(input())):
    (h, n) = map(int, input().split())
    P = list(map(int, input().split()))
    solve(h, n, P)

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

def solve(h, n, P):
    ans = 0
    dp = [float('inf')] * n
    dp[n - 1] = 0
    if n < 2:
        print(dp[0])
        return
    dp[n - 2] = 0 if P[n - 1] <= 1 else 1
    for i in range(n - 3, -1, -1):
        dp[i] = dp[i + 2] if P[i + 2] + 1 == P[i + 1] else dp[i + 1] + 1
    print(dp[0])
for _ in range(int(input())):
    (h, n) = map(int, input().split())
    P = list(map(int, input().split()))
    solve(h, n, P)