Codeforces 607A Chain Reaction(二分 + dp)
来源:互联网 发布:windows 车载 编辑:程序博客网 时间:2024/05/21 10:18
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.
题意:
有n个激光塔排成一行,第i个激光塔的位置为ai,威力是bi,当第i个激光塔被激活后,所有在这个激光塔左边且与该激光塔距离小于等于bi的激光塔都会被摧毁,而该激光塔本身不会受到伤害。管理员从右向左依次激活每个激光塔,如果一个激光塔被摧毁了,则它无法被激活。
现在管理员想让你帮他一个忙,管理员决定在现有的n个激光塔的右边再放一个激光塔,这个激光塔的位置和威力是任意的(但必须在现有激光塔的右边)。管理员从这个新加入的激光塔开始从右到左依次激活每个激光塔,现在他想要知道,怎么安排这个新激光塔,可以使得被摧毁的激光塔的总数最少。
第一行一个整数 n (1 ≤ n ≤ 100 000)
思路:
先排下序,按坐标从小到大排。
设置一个状态——第i个灯没有被破坏且[i+1, n]范围的灯全部被破坏。
用dp[i]表示该状态下信号灯被破坏的最少数量。pos表示i灯可以影响到的最左侧的灯。
得到状态转移 dp[i] = dp[pos-1] + i - pos; 在转移的时候维护最优解即可。
#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5;struct Node{ int a, b; bool operator < (Node const &other) const { return a < other.a; }}num[maxn];int dp[maxn], n;int Search(int left, int right, int val){ int mid, ans = 0; while(left <= right) { mid = (left + right) >> 1; if(num[mid].a >= val) { right = mid - 1; ans = mid; } else left = mid + 1; } return ans;}int main(){ while(cin >> n) { for(int i = 1; i <= n; i++) scanf("%d%d", &num[i].a, &num[i].b); sort(num+1, num+n+1); dp[0] = 0;int ans = 0x3f3f3f3f; for(int i = 1; i <= n; i++) { int pos = Search(1, n, num[i].a - num[i].b); dp[i] = dp[pos-1] + i - pos; ans = min(ans, dp[i] + n - i); } printf("%d\n", ans); } return 0;}
- 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 (二分)
- CodeForces 607 A. Chain Reaction(dp)
- [CodeForces 607A]Chain Reaction[DP]
- Codeforces 607A Chain Reaction【dp】
- Codeforces 607 A Chain Reaction
- 607A Chain Reaction(DP)
- Codeforces 607A:Chain Reaction 二分+递推
- Codeforces Round #336 (Div. 1) A. Chain Reaction DP+(可选二分)
- Codeforces Round #336 (Div. 1)A. Chain Reaction(DP)
- Codeforces Round #336 (Div. 1) 607A Chain Reaction 简单dp
- codeforces_607A. Chain Reaction(二分+简单dp)
- Codeforces Round #336 (Div. 2)-C Chain Reaction(二分+DP)
- Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)
- Codeforces Round #336 (Div. 1) A - Chain Reaction
- C# byte[]数组和string的互相转化 (四种方法)
- log4j配置详解
- 记录:微信项目的开始
- linux rpm包下载站
- [李景山php] 深入理解PHP内核[读书笔记]--第三章:变量及数据类型--变量的赋值和销毁
- Codeforces 607A Chain Reaction(二分 + dp)
- EL表达式格式化日期
- Hadoop之NameNode和SecondaryNameNode的区别
- BGP组网 始发端与接收端都各自有路由,但数据包双方收到对方数据(PING不通)
- 异步页面刷新pagination.js
- 命令行工具解析Crash文件,dSYM文件进行符号化
- 过拟合
- JavaScript缓冲运动之防抖动
- python线程event