天天看點

Codeforces Round #336 (Div. 1) A. Chain Reaction DP+(可選二分)

如果以位置為階段,那麼不用加二分。

如果以beacons為階段,那麼必須加二分優化。

如果beacons放置的區域範圍過大,那麼必然隻能用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 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, soai ≠ 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.

#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define MID   int mid=(le+ri)>>1
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 100000   ;
int n,dp[maxn+10];
struct Node
{
    int x,power;
    bool operator<(const Node y)const
    {
        return x<y.x;
    }
} a[maxn+10];


int bs(int le,int ri,int x)
{
    if(x<a[1].x)  return -1;

    while(le<=ri)
    {
        MID;
        if(a[mid].x>x)  ri=mid-1;
        else  le=mid+1;
    }
    return ri;
}
int  work()
{
    int ans=1;
    dp[1]=1;//對應ans=1
    for(int i=2;i<=n;i++)
    {
        int y=bs(1,i-1,a[i].x-a[i].power-1);//找出第一個<=i-a[i].power-1 的位置
        if(y<=0) {dp[i]=1;continue;}
        dp[i]=1+dp[y];
        ans=max(ans,dp[i]);
    }
    return n-ans;

}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].power);
        }
        sort(a+1,a+1+n);
        printf("%d\n",work() );
    }


   return 0;
}