Codeforces 607A Chain Reaction 【二分 + dp】
2016-01-03 12:44
543 查看
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 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.
Sample test(s)
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 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.
题意:给定n个排成一排的信号灯以及每个的位置a[]和能量b[]。若第i信号灯被打开,那么在[a[i]-b[i], a[i]-1]范围里面的信号灯都会被破坏。若现在从右向左依次打开每个信号灯(坏的不能使用),问信号灯被破坏的最少数量。你需要在所有灯的右侧放置一个新的信号灯,位置和能量随意设置。
思路:先排下序,坑我一次。O__O "…。
设置一个状态——第i个灯没有被破坏且[i+1, n]范围的灯全部被破坏。
用dp[i]表示该状态下信号灯被破坏的最少数量。pos表示i灯可以影响到的最左侧的灯。
得到状态转移 dp[i] = dp[pos-1] + i - pos; 在转移的时候维护最优解即可。
AC代码:
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 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.
Sample test(s)
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 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.
题意:给定n个排成一排的信号灯以及每个的位置a[]和能量b[]。若第i信号灯被打开,那么在[a[i]-b[i], a[i]-1]范围里面的信号灯都会被破坏。若现在从右向左依次打开每个信号灯(坏的不能使用),问信号灯被破坏的最少数量。你需要在所有灯的右侧放置一个新的信号灯,位置和能量随意设置。
思路:先排下序,坑我一次。O__O "…。
设置一个状态——第i个灯没有被破坏且[i+1, n]范围的灯全部被破坏。
用dp[i]表示该状态下信号灯被破坏的最少数量。pos表示i灯可以影响到的最左侧的灯。
得到状态转移 dp[i] = dp[pos-1] + i - pos; 在转移的时候维护最优解即可。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)
using namespace std;
struct Node{
int a, b;
};
Node num[MAXN];
bool cmp(Node A, Node B){
return A.a < B.a;
}
int dp[MAXN];
int Find(int l, int r, int val)
{
int ans = 0;
while(r >= l)
{
int mid = (l + r) >> 1;
if(num[mid].a >= val)
{
ans = mid;
r = mid-1;
}
else
l = mid+1;
}
return ans;
}
int main()
{
int n; Ri(n);
for(int i = 1; i <= n; i++)
Ri(num[i].a), Ri(num[i].b);
sort(num+1, num+n+1, cmp);
dp[0] = 0; int ans = INF;
for(int i = 1; i <= n; i++)
{
int pos = Find(1, n, num[i].a-num[i].b);
dp[i] = dp[pos-1] + i - pos;
ans = min(ans, dp[i]+n-i);
}
Pi(ans);
return 0;
}
相关文章推荐
- 在Windows下搭建React-Native Android开发环境
- react-native 保存图片到本地
- 如何加载新版本react addon
- 607A Chain Reaction(DP)
- react-native navigator
- React Native Android配置部署踩坑日记
- ACE Reactor FrameWork使用注意事项
- watchman--no-pretty get-sockname returned with exit code 1 ERROR
- [译]忘记Angular和Ember吧,React早已在前端大战中胜出
- 一看就懂的ReactJs入门教程(精华版)
- ReactNativeiOS(三)开发零碎4 Image 2 Base64
- ReactNativeiOS(三)开发零碎3 warning: Each child in ar array or integrator should have a unique "key" prop
- 【ReactNative】入门
- 最快让你上手ReactiveCocoa之基础篇
- ReactNativeiOS(三)开发零碎2 DatePickerIOS propType warning 4个
- 新的一年快开始了,学点新东西吧,从React开始(一)
- Angular2与React,前端的未来志向何方?
- 2015年终总结,忙碌与有所收获的一年,2016年继续努力!
- 为什么使用 React? Edit on GitHub
- 为什么使用 React? Edit on GitHub