Codeforces 607A Chain Reaction(二分 + dp)

A. 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 thei-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 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个激光塔排成一行,第i个激光塔的位置为ai,威力是bi,当第i个激光塔被激活后,所有在这个激光塔左边且与该激光塔距离小于等于bi的激光塔都会被摧毁,而该激光塔本身不会受到伤害。管理员从右向左依次激活每个激光塔,如果一个激光塔被摧毁了,则它无法被激活。
现在管理员想让你帮他一个忙,管理员决定在现有的n个激光塔的右边再放一个激光塔,这个激光塔的位置和威力是任意的(但必须在现有激光塔的右边)。管理员从这个新加入的激光塔开始从右到左依次激活每个激光塔,现在他想要知道,怎么安排这个新激光塔,可以使得被摧毁的激光塔的总数最少。

第一行一个整数 n (1 ≤ n ≤ 100 000) 

接下来n行,每行两个整数ai,bi ( 0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) ,数据保证不同的激光塔的位置是不一样的。
输出一个数,表示被摧毁的激光塔最少的总数。

思路:

先排下序,按坐标从小到大排。

设置一个状态——第i个灯没有被破坏且[i+1, n]范围的灯全部被破坏。

用dp[i]表示该状态下信号灯被破坏的最少数量。pos表示i灯可以影响到的最左侧的灯。

得到状态转移  dp[i] = dp[pos-1] + i - pos; 在转移的时候维护最优解即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
struct Node
{
    int a, b;
    bool operator < (Node const &other) const
    {
        return a < other.a;
    }
}num[maxn];
int dp[maxn], n;
int Search(int left, int right, int val)
{
    int mid, ans = 0;
    while(left <= right)
    {
        mid = (left + right) >> 1;
        if(num[mid].a >= val)
        {
            right = mid - 1;
            ans = mid;
        }
        else
            left = mid + 1;
    }
    return ans;
}
int main()
{
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &num[i].a, &num[i].b);
        sort(num+1, num+n+1);
        dp[0] = 0;int ans = 0x3f3f3f3f;
        for(int i = 1; i <= n; i++)
        {
            int pos = Search(1, n, num[i].a - num[i].b);
            dp[i] = dp[pos-1] + i - pos;
            ans = min(ans, dp[i] + n - i);
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值