codeforces_607A. Chain Reaction(二分+简单dp)

来源:互联网 发布:mac 容器 英文 编辑:程序博客网 时间:2024/05/16 14:00
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.

Examples
input
41 93 16 17 4
output
1
input
71 12 13 14 15 16 17 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.

二分找出每个灯塔能破坏几个已存在的灯塔,然后dp即可。不过我卡在了奇怪的地方。。一开始pos<0的情况dp居然没有置1.。。。吃个教训

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct node{    int x;    int v;    int kn;}a[maxn];bool cmp(node a,node b){    return a.x<b.x;}int dp[maxn];int l_search(int size, int key){    int first = 0, middle;    int half, len;    len = size;    while(len > 0) {        half = len >> 1;        middle = first + half;        if(a[middle].x < key) {            first = middle + 1;            len = len-half-1;        }        else            len = half;    }    return first;}int main(){    //FOP;    int i,n;    Si(n);    for(i=0;i<n;i++)    {        Si(a[i].x),Si(a[i].v);    }    sort(a,a+n,cmp);    a[0].kn=0;    int key,pos,num;    for(i=1;i<n;i++)    {        key=a[i].x-a[i].v;        pos=l_search(i,key);        num=i-pos;        a[i].kn=num;    }    dp[0]=1;    for(i=1;i<n;i++)    {        pos=i-a[i].kn-1;        if(pos<0)dp[i]=1;        dp[i]=dp[pos]+1;    }    int ma=0;    for(i=0;i<n;i++)    {        if(ma<dp[i])ma=dp[i];    }    Pi(n-ma);    return 0;}

0 0
原创粉丝点击