Codeforces 607A 动态规划

时间:2023-03-10 00:05:05
Codeforces 607A 动态规划
A. Chain Reaction
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Sample test(s)
input
4
1 9
3 1
6 1
7 4
output
1
input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
output
3
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 position1337 with power level 42.

题意:输入一个n,表示有n个灯塔,接下来输入n行数据,每一行有2个整数position,power。分别表示灯塔的坐标位置和能量值。在灯塔左边与该灯塔的距离为能量值之内(包括边界)的灯塔会熄灭,熄灭的灯塔的能量将失去作用,并且会触发该灯塔能量值范围外的第一个灯塔打开。现在所有灯塔的右边增加一个灯塔,位置任意,能量值任意。使得熄灭的灯塔的数量最少。

思路:power[i]表示位置i的灯塔的能量值,dp[i]表示从0到i有多少个灯塔打开。如果i位置有一个灯塔的话dp[i]=dp[i-power[i]-1]+1;如果i-power[i]-1<0的话表示该灯塔左边的灯塔将会全部熄灭,那就只剩下自己,此时dp[i]=1;

#include<bits/stdc++.h>
using namespace std;
int power[],dp[];
int main()
{
int i,n,pos,MPos=;
int Max;
scanf("%d",&n);
memset(power,,sizeof(power));
for(i=;i<n;i++)
{
scanf("%d",&pos);
scanf("%d",&power[pos]);
if(pos>MPos) MPos=pos;
}
memset(dp,,sizeof(dp));
if(power[]>) dp[]=;
Max=dp[];
for(i=; i<=MPos; i++)
{
if(power[i]==) dp[i]=dp[i-];
else
{
if((i-power[i]-)>=)
dp[i]=dp[i-power[i]-]+;
else dp[i]=;
}
if(dp[i]>Max) Max=dp[i];
}
cout<<n-Max<<endl;
return ;
}