您的位置:首页 > Web前端 > React

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

2015-12-26 17:30 543 查看
[align=center]C. Chain Reaction[/align]
[align=center]time limit per test[/align]
[align=center]2 seconds[/align]
[align=center]memory limit per test[/align]
[align=center]256 megabytes[/align]
[align=center]input[/align]
[align=center]standard input[/align]
[align=center]output[/align]
[align=center]standard output[/align]

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.

题意:琦玉想让杰诺斯从右到左点亮灯塔,但每个灯塔都有自己的坐标ai和能量bi,当一座灯塔被点亮时,在该灯塔左边bi范围内的灯塔会被摧毁,杰诺斯可以选择一个起点来点亮灯塔,起点被视为一座灯塔,可以是已给出的灯塔或者是自定义坐标和能量的灯塔,求最少会有多少灯塔被摧毁。
思路:dp,如果用二分去求存活多少座也是可以的,不过题解直接用数组b坐标表示范围,b[i]表示能量。

dp[i]表示点亮第i座灯塔会存活多少座灯塔,那么就是求要开始时先摧毁多少座灯塔最后能使得摧毁量最少(或存活灯塔最多)

dp[i] = dp[i-1], b[i] = 0;

dp[i] = 1, b[i]>=I;

dp[i] = dp[i - b[i] - 1] + 1;

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 1000005
int b[maxn], dp[maxn];
int main()
{
int n, i, a, res = 0;
scanf("%d", &n);
for(i = 0;i < n;i++)
{
scanf("%d", &a);
scanf("%d", &b[a]);
}
if(b[0] > 0) dp[0] = 1;
for(i = 1;i < maxn;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;
}
if(dp[i] > res) res = dp[i];
}
printf("%d\n", n-res);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 动态规划