Codeforces Round #336 (Div. 2)-C. Chain Reaction
2016-08-26 16:39
267 查看
原题链接
C. 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
output
input
output
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 9with
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
先对地点从小到大排序,对于每个地点i, 算出activated后0到i-1destroyed的数目dp[i]
= dp[l] + i - l - 1.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;
struct Node{
Node(){
}
Node(int p1, int p2){
a = p1;
b = p2;
}
int a, b;
friend bool operator < (const Node&k1, const Node&k2){
return k1.a < k2.a;
}
}node[maxn];
int dp[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, a, b;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d%d", &a, &b);
node[i] = Node(a, b);
}
sort(node, node+n);
int ans = n - 1;
dp[0] = 0;
for(int i = 1; i < n; i++){
int f = node[i].a - node[i].b;
if(f > node[0].a){
int l = 0, r = i;
while(l < r){
int mid = (l + r) >> 1;
if(node[mid].a >= f)
r = mid;
else
l = mid + 1;
}
l--;
dp[i] = dp[l] + i - l - 1;
ans = min(ans, dp[i] + n - i - 1);
}
else
dp[i] = i;
}
printf("%d\n", ans);
return 0;
}
C. 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 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 9with
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
先对地点从小到大排序,对于每个地点i, 算出activated后0到i-1destroyed的数目dp[i]
= dp[l] + i - l - 1.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define MOD 10007
#define maxn 100005
using namespace std;
typedef long long ll;
struct Node{
Node(){
}
Node(int p1, int p2){
a = p1;
b = p2;
}
int a, b;
friend bool operator < (const Node&k1, const Node&k2){
return k1.a < k2.a;
}
}node[maxn];
int dp[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, a, b;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d%d", &a, &b);
node[i] = Node(a, b);
}
sort(node, node+n);
int ans = n - 1;
dp[0] = 0;
for(int i = 1; i < n; i++){
int f = node[i].a - node[i].b;
if(f > node[0].a){
int l = 0, r = i;
while(l < r){
int mid = (l + r) >> 1;
if(node[mid].a >= f)
r = mid;
else
l = mid + 1;
}
l--;
dp[i] = dp[l] + i - l - 1;
ans = min(ans, dp[i] + n - i - 1);
}
else
dp[i] = i;
}
printf("%d\n", ans);
return 0;
}
相关文章推荐
- [Mac]Mac OS 10.11虚拟机搭建ReactNative遇坑记录
- 在ASP.NET MVC项目中使用React
- React native 第一个坑记录
- ReactNative 实现图片轮播器
- react入门(1)
- PHP获取不了React Native Fecth参数的解决办法
- React/React Native 的ES5 ES6写法对照表
- 【稀饭】react native 实战系列教程之首页列表UI实现
- 【稀饭】react native 实战系列教程之项目初始化
- 关于Redux的一些总结(一):Action & 中间件 & 异步
- React-组件的生命周期
- 混合开发的大趋势之一React Native ScrollView (拉伸视图), ListView(列表视图)
- React Native商城项目实战15 - 首页购物中心
- 前端小结----Webpack React
- 深入浅出React(二):React开发神器Webpack
- React+Webpack快速上手指南
- ReactJs和React Native的那些事
- ReactJs和React Native的那些事
- ReactJs和React Native的那些事
- IOS与ReactNative