Codeforces 607A Chain Reaction 【二分 + dp】

91 篇文章 0 订阅
62 篇文章 0 订阅

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个排成一排的信号灯以及每个的位置a[]和能量b[]。若第i信号灯被打开,那么在[a[i]-b[i], a[i]-1]范围里面的信号灯都会被破坏。若现在从右向左依次打开每个信号灯(坏的不能使用),问信号灯被破坏的最少数量。你需要在所有灯的右侧放置一个新的信号灯,位置和能量随意设置。


思路:先排下序,坑我一次。O__O "…。

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

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

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


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Node{
    int a, b;
};
Node num[MAXN];
bool cmp(Node A, Node B){
    return A.a < B.a;
}
int dp[MAXN];
int Find(int l, int r, int val)
{
    int ans = 0;
    while(r >= l)
    {
        int mid = (l + r) >> 1;
        if(num[mid].a >= val)
        {
            ans = mid;
            r = mid-1;
        }
        else
            l = mid+1;
    }
    return ans;
}
int main()
{
    int n; Ri(n);
    for(int i = 1; i <= n; i++)
        Ri(num[i].a), Ri(num[i].b);
    sort(num+1, num+n+1, cmp);
    dp[0] = 0; int ans = INF;
    for(int i = 1; i <= n; i++)
    {
        int pos = Find(1, n, num[i].a-num[i].b);
        dp[i] = dp[pos-1] + i - pos;
        ans = min(ans, dp[i]+n-i);
    }
    Pi(ans);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值