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, 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.
题意:
有很多个基佬,他们都有他自己的位置a和自己的魅力值b,每一个基佬可以py左边b单位的基佬,如果一个基佬被py了,他就不能去py别的基佬,现在你有一次py从右边第一个基佬起的任意个连续的基佬.
题解:
首先很容易想到暴力是吧,从右边第一个基佬起开始枚举,然后每次都搜一遍,复杂度n*n,在枚举的过程中,我们可以略加思考,每个dp[i]值其实就等于1+dp[j],j:他不能py到的第一个基佬,故我们可以从左到右开始py,dp[i]:最多可以保留多少个基佬,然后最后再min一下就行><.
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
pair<int, int>arr[100010];
int n, dp[100010],ans=0x3f3f3f;
bool cmp(pair<int, int>a, pair<int, int>b)
{
return a.first < b.first;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> arr[i].first >> arr[i].second;
}
sort(arr + 1, arr + 1 + n,cmp);
for (int i = 1; i <= n; i++)
{
int temp = lower_bound(arr + 1, arr + 1 + i, make_pair(arr[i].first - arr[i].second,-1))-arr-1;
if (arr[temp].first == arr[i].first - arr[i].second)temp--;
dp[i] = 1 + dp[temp];
ans = min(ans, n-dp[i]);
}
cout << ans << endl;
return 0;
}