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);
}
[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);
}
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C#使用动态规划解决0-1背包问题实例分析
- 动态规划
- C++ 动态规划
- DP(动态规划) 解游轮费用问题
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 关于爬楼梯的动态规划算法
- 动态规划 --- hdu 1003 **
- DP问题各种模型的状态转移方程
- 0-1背包解题过程
- 背包问题
- USACO 3.2.2:Stringsobits
- 字符串编辑距离
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)