Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)

来源:互联网 发布:sql 保留小数位数 编辑:程序博客网 时间:2024/04/29 03:58

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 0001 ≤ 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 test(s)
input
41 93 16 17 4
output
1
input
71 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个点的坐标以及照射范围, 现要在最右边放置一个灯塔, 位置和照射范围任意, 问最少有几个灯塔可以被摧毁.

dp数组记录第i个灯塔存在时照亮的数量, 求出每个灯塔摧毁范围同时更新dp和记录最大的dp, 范围大于0则等于那个点照亮数量围, 且自

己伤害范围不为0时, 再把自己算进去.

AC代码:

#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"#include "queue"#include "stack"#include "cmath"#include "utility"#include "map"#include "set"#include "vector"#include "list"#include "string"#include "cstdlib"using namespace std;typedef long long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 1e6 + 6;int n, dp[MAXN], magic[MAXN], ans;int main(int argc, char const *argv[]){scanf("%d", &n);for(int i = 0; i < n; ++i) {int x, y;scanf("%d%d", &x, &y);magic[x] = y;}for(int i = 0; i <= 1e6; ++i) {int x = i - magic[i] - 1;if(x < 0) dp[i] = 0;else dp[i] = dp[x];if(magic[i]) dp[i]++;if(dp[i] > ans) ans = dp[i];}printf("%d\n", n - ans);return 0;}

 

1 1