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.
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.
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
思路:dp,如果用二分去求存活多少座也是可以的,不过题解直接用数组b坐标表示范围,b[i]表示能量。
dp[i]表示点亮第i座灯塔会存活多少座灯塔,那么就是求要开始时先摧毁多少座灯塔最后能使得摧毁量最少(或存活灯塔最多)
dp[i] = dp[i-1], b[i] = 0;
dp[i] = 1, b[i]>=I;
dp[i] = dp[i - b[i] - 1] + 1;
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 1000005
int b[maxn], dp[maxn];
int main()
{
int n, i, a, res = 0;
scanf("%d", &n);
for(i = 0;i < n;i++)
{
scanf("%d", &a);
scanf("%d", &b[a]);
}
if(b[0] > 0) dp[0] = 1;
for(i = 1;i < maxn;i++)
{
if(b[i] == 0) dp[i] = dp[i - 1];
else
{
if(b[i] >= i) dp[i] = 1;
else dp[i] = dp[i - b[i] - 1] + 1;
}
if(dp[i] > res) res = dp[i];
}
printf("%d\n", n-res);
}