codeforces 607 A Chain Reaction (二分)
2016-03-10 21:43
591 查看
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
output
input
output
solution:
我们发现,我们新灯塔的功能,其实是使得最右面的若干个灯塔失效。
意思就是,使得第1,2,3,4,5,……,n个灯塔,作为没被破坏的最后一个灯塔。
我们能否算出第i个灯塔,作为没被破坏的最后一个灯塔条件下的答案呢?
这个显然是很容易做到的。我们可以按照灯塔坐标对这n个灯塔做升序排序。
然后,对于第i个灯塔,它可以破坏掉的区间范围是a[i].first-a[i].second
设最后一个破坏掉的位置为pos,那么我们在a中做二分, 查找第一个位置比pos小的。之前的便全部保留了。
基于这个方式,我们就可以在O(nlogn)的条件下,枚举最后一个没被破坏的灯塔,暴力过掉这道题了。
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 12 13 14 15 16 17 1
output
3
solution:
我们发现,我们新灯塔的功能,其实是使得最右面的若干个灯塔失效。
意思就是,使得第1,2,3,4,5,……,n个灯塔,作为没被破坏的最后一个灯塔。
我们能否算出第i个灯塔,作为没被破坏的最后一个灯塔条件下的答案呢?
这个显然是很容易做到的。我们可以按照灯塔坐标对这n个灯塔做升序排序。
然后,对于第i个灯塔,它可以破坏掉的区间范围是a[i].first-a[i].second
设最后一个破坏掉的位置为pos,那么我们在a中做二分, 查找第一个位置比pos小的。之前的便全部保留了。
基于这个方式,我们就可以在O(nlogn)的条件下,枚举最后一个没被破坏的灯塔,暴力过掉这道题了。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<string> #include<iostream> #include<set> using namespace std; typedef long long ll; #define mem(a) (memset(a,0,sizeof(a))) #define f0(n) for(int i=0;i<n;i++) #define f1(n) for(int i=1;i<=n;i++) #define si(x) scanf("%d",&x) #define sl(x) scanf("%I64d",&x) #define sd(x) scanf("%lf",&x) #define sii(x,y) scanf("%d%d",&x,&y) #define siii(x,y,z) scanf("%d%d%d",&x,&y,&z) #define mp(x,y) make_pair(x,y) const int maxn = 1e6 + 500; int n, m; pair<int, int>a[maxn]; int res[maxn]; int main() { si(n); f1(n) sii(a[i].first, a[i].second); sort(a + 1, a + n + 1); int ans =maxn; for (int i = 1; i <= n; i++) { int pos = lower_bound(a + 1, a + i, mp(a[i].first - a[i].second, -1)) - 1 - a; res[i] = res[pos] + 1; ans = min(ans, n - res[i]); } printf("%d\n", ans); return 0; }
相关文章推荐
- angular到react
- native、ionic和react-native优劣势对比
- ReactiveCocoa 学习心得 -- 2
- 多线程处理的几种模式—Proactor和Reactor
- Reactor模型 三种模型 演变
- Netty的Reactor线程模型
- reactor/proactor模型简介
- Windows下React Native的Android环境部署及布局示例
- react-native在Anroid真机运行时可能会遇到白屏的情况解决办法
- React-Native自学资料汇总
- React
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
- React-Native系列Android——Win7系统环境搭建问题汇总
- 认识react-native
- 学习iOS、React Native、算法开发的建议
- redux+react+webpack+热加载+兼容IE8(持续更新)
- react tutorial comment遇到的问题解决
- React-表单详解
- ReactiveCocoa - iOS开发的新框架
- React Native for Android 学习