CodeForces - 608C - Chain Reaction (dp & 二分)

题目链接
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 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.

Examples
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 position 1337 with power level 42.

题目是说在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围。现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭。否则会被激发,留下自己。
现在可以从右边放置一个灯塔,位置和power level都可以自己定义。问各种情况中最小的灯塔被毁灭的数量。
这个题目要用dp,dp[i]代表前 i 个中第 i 个被激活的时候,最多存留的数量。看他前面的范围外的第一个,这里找这一个的时候需要用二分,如果能找到,那么就在此基础上加1,如果找不到那么就说明到此为止只有自己能存活。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
using namespace std;
const int maxn = 1e5 + 5, inf = 0x3f3f3f3f;
pair<int, int> a[maxn];
int b[maxn], dp[maxn];
int n;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d%d", &a[i].fi, &a[i].se);
    sort(a, a + n);
    for(int i = 0; i < n; i++)  b[i] = a[i].fi;
    int ans = 1;
    dp[0] = 1;
    for(int i = 1; i < n; i++)
    {
        int temp = lower_bound(b, b + i + 1, a[i].fi - a[i].se) - b;
        if(!temp)   dp[i] = 1;
        else    dp[i] = dp[temp - 1] + 1;
        ans = max(ans, dp[i]);
    }
    printf("%d\n", n - ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值