Chain Reaction (Codeforces 608C)
Tags: DP, dp

There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input: The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output: Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Note: For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.

Sample Inputs:
4
1 9
3 1
6 1
7 4

7
1 1
2 1
3 1
4 1
5 1
6 1
7 1

Sample Outputs:
1

3

Solution (Python):
n = int(input())
arr = [0] * 1000001
dp = [0] * 1000001
for i in range(n):
    line = input().split()
    a = int(line[0])
    b = int(line[1])
    arr[a] = b
if arr[0] > 0:
    dp[0] = 1
for i in range(1, 1000001):
    if arr[i] == 0:
        dp[i] = dp[i - 1]
    elif arr[i] >= i:
        dp[i] = 1
    else:
        dp[i] = dp[i - arr[i] - 1] + 1
print(n - max(dp))

Solution (Python):
from sys import stdin, stdout

def ChainReaction(n, b):
    survivors = 0
    dp = [0] * 1000001
    dp[0] = 0 if b[0] == 0 else 1
    for i in range(1000001):
        if b[i] == 0:
            dp[i] = dp[i - 1]
        elif b[i] >= i:
            dp[i] = 1
        else:
            dp[i] = dp[i - b[i] - 1] + 1
        if dp[i] > survivors:
            survivors = dp[i]
    return n - survivors

def main():
    n = int(stdin.readline().strip())
    arr = [0] * 1000001
    for i in range(n):
        a = [int(x) for x in stdin.readline().split()]
        arr[a[0]] = a[1]
    print(ChainReaction(n, arr))
if __name__ == '__main__':
    main()

Solution (Python):
import sys
maxN = 10 ** 6 + 5
dp = [0] * maxN
b = [0] * maxN
N = int(sys.stdin.readline())
for _ in range(N):
    beacon = [int(x) for x in sys.stdin.readline().split()]
    b[beacon[0]] = beacon[1]
if b[0] > 0:
    dp[0] = 1
for i in range(1, maxN):
    if b[i] == 0:
        dp[i] = dp[i - 1]
    elif b[i] >= i:
        dp[i] = 1
    else:
        dp[i] = dp[i - b[i] - 1] + 1
print(N - max(dp))