Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp
2015-12-25 11:03
357 查看
C. Chain Reaction
题目连接:
http://www.codeforces.com/contest/608/problem/CDescription
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.Sample Input
41 9
3 1
6 1
7 4
Sample Output
1Hint
题意
在一个数轴上,有n根杆子,每根杆子在ai位置,高bi有一个人在推杆子,然后杆子会向左边倒下,使得被压中的杆子都会坏掉
然后让你在最右边加一个杆子,高度自己定
使得坏掉的杆子数量最少,问你最少的数量是多少
题解:
我们只要扫一遍如果推倒这个杆子,最多剩余数量是多少就好了毁掉的 = n - 没毁掉的
dp[a[i]] = dp[a[i]-b[i]] + 1
这个你可以把每个坐标扫一遍,也可以套个数据结构log转移
代码
#include<bits/stdc++.h> using namespace std; #define maxn 100005 set<pair<int,int> >S; pair<int,int> p[maxn]; int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].first,&p[i].second); int ans = 0; sort(p+1,p+n+1); S.insert(make_pair(-1,0)); for(int i=1;i<=n;i++) { pair<int,int> T = *--S.lower_bound(make_pair(p[i].first-p[i].second,-5)); ans = max(ans,T.second+1); S.insert(make_pair(p[i].first,T.second+1)); } cout<<n-ans<<endl; }
相关文章推荐
- 利用CocoaPods集成ReactiveCocoa
- Codeforces Round #336 (Div. 1) 607A Chain Reaction 简单dp
- React 学习一 运行
- React测试Mixin
- windows下react-native android环境安装
- React组件测试(模拟组件、函数和事件)
- Codeforces 607A:Chain Reaction 二分+递推
- 用React.addons.TestUtils、Jasmine进行单元测试
- 基于React实现项目中状态型变量的数据驱动可视化
- react 写的省市三级联动
- ReactiveCocoa
- 手把手教你在Windows下搭建React Native Android开发环境
- 手把手教你在Windows下搭建React Native Android开发环境
- ReactJs入门思路小指南
- 初识React
- 手把手教你在Windows下搭建React Native Android开发环境
- Codeforces Round #336 (Div. 2)C. Chain Reaction DP
- ANGULAR 2 FOR REACT DEVELOPERS
- React with TypeScript 系列(一) --概述
- webpack使用babel 6打包react报错