Codeforces Round #336 (Div. 2) C 二分+dp

来源:互联网 发布:精神感应高达数据 编辑:程序博客网 时间:2024/05/21 17:54



链接:戳这里


C. Chain Reaction
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

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.


题意:

给出n个游标,放在x轴上,每个游标有一个激活装置,激活之后能把左边ai范围内的游标全炸了

现在要求你在所有游标的最右边安放一个游标,使的摧毁的游标最少。注意游标会从右往左一一激活

激活的游标在炸了右边的游标之后本身不会被摧毁


思路:

对于每个游标,相当于把一个线段[l,r]铺在x轴上,然后拿掉最少数量的游标使得线段不重复

那么就是对n个区间[l,r]进行取舍的问题了,我们先按区间的r排个序

接下来我们设置dp[]状态表示当前第i个区间作为最后一个游标的最优值(也就是保存的游标数量最大)

那么当前游标能影响的游标也就是[1,i-1]里面能覆盖的个数,用二分找到当前i能覆盖的最远距离(也就是最远的rj 1<=j<=i-1)

减去最远距离到i的游标个数,在取dp[mid]+1  更新最大的ans


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;int n;struct node{    int l,r;}s[100100];bool cmp(node a,node b){    if(a.r==b.r) return a.l<b.l;    return a.r<b.r;}int dp[1000100];int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++){        int x,rang;        scanf("%d%d",&x,&rang);        s[i].l=x-rang;        s[i].r=x;    }    sort(s+1,s+n+1,cmp);    dp[0]=1;    s[0].l=-1000000000;    s[0].r=-1000000000;    int ans=0;    for(int i=1;i<=n;i++){        int l=0,r=i-1,mid,f=-1;        while(l<=r){            mid=(l+r)/2;            if(s[mid].r<s[i].l) {                l=mid+1;                f=mid;            }            else if(s[mid].r==s[i].l) r=mid-1;            else r=mid-1;        }        dp[i]=dp[f]+1;        ans=max(ans,dp[i]);    }    cout<<n+1-ans<<endl;    return 0;}



0 0
原创粉丝点击