Codeforces Round #336 (Div. 2) C. Chain Reaction

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

题意:琦玉想让杰诺斯从右到左点亮灯塔,但每个灯塔都有自己的坐标ai和能量bi,当一座灯塔被点亮时,在该灯塔左边bi范围内的灯塔会被摧毁,杰诺斯可以选择一个起点来点亮灯塔,起点被视为一座灯塔,可以是已给出的灯塔或者是自定义坐标和能量的灯塔,求最少会有多少灯塔被摧毁。

思路:dp,如果用二分去求存活多少座也是可以的,不过题解直接用数组b坐标表示范围,b[i]表示能量。

dp[i]表示点亮第i座灯塔会存活多少座灯塔,那么就是求要开始时先摧毁多少座灯塔最后能使得摧毁量最少(或存活灯塔最多)

dp[i] = dp[i-1], b[i] = 0;

dp[i] = 1, b[i]>=I;

dp[i] = dp[i - b[i] - 1] + 1;

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 1000005
int b[maxn], dp[maxn];
int main()
{
    int n, i, a, res = 0;
    scanf("%d", &n);
    for(i = 0;i < n;i++)
    {
        scanf("%d", &a);
        scanf("%d", &b[a]);
    }
    if(b[0] > 0) dp[0] = 1;
    for(i = 1;i < maxn;i++)
    {
        if(b[i] == 0) dp[i] = dp[i - 1];
        else
        {
            if(b[i] >= i) dp[i] = 1;
            else dp[i] = dp[i - b[i] - 1] + 1;
        }
        if(dp[i] > res) res = dp[i];
    }
    printf("%d\n", n-res);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值