codeforces_607A. Chain Reaction(二分+简单dp)
来源:互联网 发布:mac 容器 英文 编辑:程序博客网 时间:2024/05/16 14:00
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.
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.
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
41 93 16 17 4
1
71 12 13 14 15 16 17 1
3
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.
二分找出每个灯塔能破坏几个已存在的灯塔,然后dp即可。不过我卡在了奇怪的地方。。一开始pos<0的情况dp居然没有置1.。。。吃个教训
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct node{ int x; int v; int kn;}a[maxn];bool cmp(node a,node b){ return a.x<b.x;}int dp[maxn];int l_search(int size, int key){ int first = 0, middle; int half, len; len = size; while(len > 0) { half = len >> 1; middle = first + half; if(a[middle].x < key) { first = middle + 1; len = len-half-1; } else len = half; } return first;}int main(){ //FOP; int i,n; Si(n); for(i=0;i<n;i++) { Si(a[i].x),Si(a[i].v); } sort(a,a+n,cmp); a[0].kn=0; int key,pos,num; for(i=1;i<n;i++) { key=a[i].x-a[i].v; pos=l_search(i,key); num=i-pos; a[i].kn=num; } dp[0]=1; for(i=1;i<n;i++) { pos=i-a[i].kn-1; if(pos<0)dp[i]=1; dp[i]=dp[pos]+1; } int ma=0; for(i=0;i<n;i++) { if(ma<dp[i])ma=dp[i]; } Pi(n-ma); return 0;}
- codeforces_607A. Chain Reaction(二分+简单dp)
- Codeforces 607A Chain Reaction 【二分 + dp】
- Codeforces 607A Chain Reaction (dp+二分)
- codeforces 607 A Chain Reaction dp 二分
- Codeforces 607A Chain Reaction(二分 + dp)
- CodeForces 607 A. Chain Reaction(dp)
- Codeforces Round #336 (Div. 2)-C Chain Reaction(二分+DP)
- 607A Chain Reaction(DP)
- Codeforces Round #336 (Div. 1)A. Chain Reaction(DP)
- Codeforces Round #336 (Div. 1) A. Chain Reaction DP+(可选二分)
- cf#336-C - Chain Reaction-二分
- codeforces 607 A Chain Reaction (二分)
- [CodeForces 607A]Chain Reaction[DP]
- Codeforces 607A Chain Reaction【dp】
- Codeforces Round #336 (Div. 1) 607A Chain Reaction 简单dp
- Chain reaction
- Codeforces 607A:Chain Reaction 二分+递推
- Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)
- android xml解析
- c#中Thread的基本使用
- 【软件工程】 文档 - 银行业务管理 - 面向对象分析与设计
- python爬虫
- ubuntu14.04中科大安装源
- codeforces_607A. Chain Reaction(二分+简单dp)
- HDU5695-Gym Class-拓扑排序优先队列结合
- Python Skelve 库
- Freda的传呼机
- ubuntu上安装MySQL非常简单只需要几条命令就可以完成。
- 高效加载单个大图片——OOM
- STL::generate、generate_n用法
- 一步一图一代码,一定要让你真正彻底明白红黑树
- jdk源码分析之HashMap