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 a i and power level b i. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance b i 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 a i and b i (0 ≤ a i ≤ 1 000 000, 1 ≤ b i ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so a i ≠ a j if i ≠ j.
Output
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
<코드>
#include <stdio.h>
#include<algorithm>
using namespace std;
int const N = 1e6 + 1;
int n, dp[N], b[N];
int main() {
scanf("%d", &n);
for (int i = 0, a; i < n; ++i) {
scanf("%d", &a);
scanf("%d", b + a);
}
int res = 0;
dp[0] = b[0] != 0;
for (int i = 1; i < N; ++i) {
if (b[i] == 0)
dp[i] = dp[i - 1];
else {
if (b[i] >= i)
dp[i] = 1;
else
dp[i] = dp[i - b[i] - 1] + 1;
}
res = max(res, dp[i]);
}
printf("%d\n", n - res);
return 0;
}
https://codeforces.com/problemset/problem/607/A
'🧩PS > 🥇Hard' 카테고리의 다른 글
[C/C++] 백준 2178번 미로탐색 (BFS, DFS) (0) | 2020.05.02 |
---|---|
[C/C++] Codeforce 602B - Approximating a Constant Range (0) | 2020.04.30 |
[C/C++] Codeforce 706C - Hard Problem (0) | 2020.04.30 |
[C/C++] Codeforce 327A - Flipping Game (0) | 2020.04.30 |
[C/C++] Codeforce 455A - Boredom (DP) (0) | 2020.04.30 |