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.
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 position 1337 with power level 42.
题目大意:
给你N个灯塔,接下来N行,每行两个元素,表示灯塔的位子和能量范围。当一个灯塔建立的时候,其左边能量范围内的灯塔,将被摧毁,如果此灯塔没有建立的时候,其没有作用,此时我们可以在最初的时候,所有灯塔的最右边建立一个灯塔,位子和能量范围是任意设定的。问最少有多少灯塔被摧毁。
思路:
1、考虑dp,如果我们考虑dp最少摧毁的数量,显然比较困难,考虑逆向思维,我们设定dp【i】表示从0-i位子中最多建立的灯塔数,那么其区间【0-i】最少摧毁的灯塔数即已知。
2、考虑其状态转移方程(其中a【i】=x表示位子i上灯塔的作用范围为x):
①如果此灯塔是第一个灯塔,那么dp【i】=1,表示这个灯塔左侧没有灯塔能被其摧毁。
②如果此灯塔的作用范围能够覆盖到第一个灯塔,那么dp【i】=1,表示如果建立此灯塔,那么其左侧全部灯塔都被摧毁了,那么区间【0-i】内,只能建立当前一个灯塔。
③如果以上两条都不符合,那么dp【i】=1+dp【i-a【i】-1】表示,当前灯塔建立(1),并且累加上其作用范围以外的区间【0,i-a【i】-1】最多的灯塔数(dp【i-a【i】-1】)。
④如果当前位子i没有灯塔,那么dp【i】=dp【i-1】。
3、初始建立一个灯塔在所有灯塔的最右侧其位子和作用范围任意,其实就是在说,建立完这个灯塔之后,其最右端的一些灯塔就被摧毁了,那么我们最多建立的灯塔数,就是相当于,在从【0,i】取一个最大值(0<=i<=1000000),那么最小摧毁数=n-最大建立数。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[1000040];
int dp[1000040];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
int pos,r;
scanf("%d%d",&pos,&r);
a[pos]=r;
}
int f=0,posf;
for(int i=0;i<=1000000;i++)
{
if(a[i]==0)dp[i]=dp[i-1];
else
{
if(f==0)dp[i]=1,f++,posf=i;
else if(i-posf<=a[i])dp[i]=1;
else dp[i]=dp[i-a[i]-1]+1;
}
}
int output=0;
for(int i=0;i<=1000000;i++)
{
output=max(output,dp[i]);
}
printf("%d\n",n-output);
}
}
探讨了在一个数轴上放置和激活灯塔的问题,目标是最小化因新增灯塔而被摧毁的原有灯塔数量。

1059

被折叠的 条评论
为什么被折叠?



