Codeforces 607A 动态规划
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 thei-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.
4
1 9
3 1
6 1
7 4
1
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
3
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;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int power[1000010],dp[1000010]; int main() { int i,n,pos,MPos=0; int Max; scanf("%d",&n); memset(power,0,sizeof(power)); for(i=0;i<n;i++) { scanf("%d",&pos); scanf("%d",&power[pos]); if(pos>MPos) MPos=pos; } memset(dp,0,sizeof(dp)); if(power[0]>0) dp[0]=1; Max=dp[0]; for(i=1; i<=MPos; i++) { if(power[i]==0) dp[i]=dp[i-1]; else { if((i-power[i]-1)>=0) dp[i]=dp[i-power[i]-1]+1; else dp[i]=1; } if(dp[i]>Max) Max=dp[i]; } cout<<n-Max<<endl; return 0; }