【Codeforces Round 336 (Div 2) C】【贪心 DP思维】Chain Reaction 每个灯塔位置为a[]破坏b[]范围所有灯塔 设置一个灯塔使得最多灯塔被保留

C. 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 0001 ≤ 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.

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 9with 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.



#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int casenum,casei;
int n,x,y;
pair<int,int>a[N];
int res[N];
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;++i)scanf("%d%d",&a[i].first,&a[i].second);
		sort(a+1,a+n+1);
		a[n+1].first=-1;
		res[0]=0;

		//枚举每个位置作为最后不被破坏的位置
		int ans=1e9;
		for(int i=1;i<=n;++i)
		{
			int last=lower_bound(a+1,a+i,MP(a[i].first-a[i].second,-1))-1-a;//最后一个不被它破坏的位置
			res[i]=res[last]+1;
			gmin(ans,n-res[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
我竟然卡在这道题上这么久!
在你想不到好的做法的时候,试试看最原始的暴力!

【题意】
有n(1e5)个灯塔,每个灯塔有属性(坐标a[i].first,向左的照射范围a[i].second),所有灯塔坐标两两不同。
我们从右往左,对于每个灯塔,其所照射到的所有灯塔都被认定为被破坏。
我们现在可以在所有灯塔的右边,布置一个新的灯塔,位置和范围任意定。

问你,是否有一种布置灯塔的方案,可以使得被破坏的灯塔数尽可能少,并输出最少破坏的灯塔数。

【类型】
贪心 DP思维

【分析】

尝试暴力!
我们发现,我们新灯塔的功能,其实是使得最右面的若干个灯塔失效。
意思就是,使得第1,2,3,4,5,……,n个灯塔,作为没被破坏的最后一个灯塔。

我们能否算出第i个灯塔,作为没被破坏的最后一个灯塔条件下的答案呢?
这个显然是很容易做到的。我们可以按照灯塔坐标对这n个灯塔做升序排序。

然后,对于第i个灯塔,它可以破坏掉的区间范围是a[i].first-a[i].second
设最后一个破坏掉的位置为pos,那么我们在a中做二分, 查找第一个位置比pos小的。之前的便全部保留了。

基于这个方式,我们就可以在O(nlogn)的条件下,枚举最后一个没被破坏的灯塔,暴力过掉这道题了。

【时间复杂度&&优化】
O(nlogn)

*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值