Codeforces 607A 动态规划
2 seconds
256 megabytes
standard input
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.
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.
4
1 9
3 1
6 1
7 4
1
7
1 1
2 1
3 1
4 1
5 1
6 1
7 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,表示有n个灯塔,接下来输入n行数据,每一行有2个整数position,power。分别表示灯塔的坐标位置和能量值。在灯塔左边与该灯塔的距离为能量值之内(包括边界)的灯塔会熄灭,熄灭的灯塔的能量将失去作用,并且会触发该灯塔能量值范围外的第一个灯塔打开。现在所有灯塔的右边增加一个灯塔,位置任意,能量值任意。使得熄灭的灯塔的数量最少。
思路:power[i]表示位置i的灯塔的能量值,dp[i]表示从0到i有多少个灯塔打开。如果i位置有一个灯塔的话dp[i]=dp[i-power[i]-1]+1;如果i-power[i]-1<0的话表示该灯塔左边的灯塔将会全部熄灭,那就只剩下自己,此时dp[i]=1;
#include<bits/stdc++.h>
using namespace std;
int power[],dp[];
int main()
{
int i,n,pos,MPos=;
int Max;
scanf("%d",&n);
memset(power,,sizeof(power));
for(i=;i<n;i++)
{
scanf("%d",&pos);
scanf("%d",&power[pos]);
if(pos>MPos) MPos=pos;
}
memset(dp,,sizeof(dp));
if(power[]>) dp[]=;
Max=dp[];
for(i=; i<=MPos; i++)
{
if(power[i]==) dp[i]=dp[i-];
else
{
if((i-power[i]-)>=)
dp[i]=dp[i-power[i]-]+;
else dp[i]=;
}
if(dp[i]>Max) Max=dp[i];
}
cout<<n-Max<<endl;
return ;
}
Codeforces 607A 动态规划的更多相关文章
- Codeforces 837D 动态规划
Codeforces 837D 动态规划 传送门:https://codeforces.com/contest/837/problem/D 题意: 给你n个数,问你从这n个数中取出k个数,这k个数的乘 ...
- codeforces 1183H 动态规划
codeforces 1183H 动态规划 传送门:https://codeforces.com/contest/1183/problem/H 题意: 给你一串长度为n的字符串,你需要寻找出他的最长的 ...
- Codeforces 607A - Chain Reaction - [DP+二分]
题目链接:https://codeforces.com/problemset/problem/607/A 题意: 有 $n$ 个塔排成一行,第 $i$ 个激光塔的位置为 $a_i$,伤害范围是 $b_ ...
- Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...
- CODEFORCES 429B 动态规划
http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...
- CodeForces 366C 动态规划 转化背包思想
这道题目昨晚比赛没做出来,昨晚隐约觉得就是个动态规划,但是没想到怎么DP,今天想了一下,突然有个点子,即局部最优子结构为 1-j,j<i,遍历i,每次从所有的1到j当中的最优解里面与当前商品进行 ...
- Comb CodeForces - 46E (动态规划)
题面 Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To h ...
- 2018省赛赛第一次训练题解和ac代码
第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title A CodeForces 607A Chain Reaction B CodeForces ...
- Codeforces Flipping game 动态规划基础
题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...
随机推荐
- 进程池----Pool(老的方式)----回调
之后的进程池使用的是 ProcessPoolExecutor,它的底层使用的就是pool 为什么要有进程池?进程池的概念. 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星 ...
- create a bootable USB stick on Ubuntu
https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu?_ga=2.141187314.17572770 ...
- WPF 颜色拾色器
效果图: 下载:Code 参考: http://www.codeproject.com/Articles/33001/WPF-A-Simple-Color-Picker-With-Previewhtt ...
- docker 带参数启动 配合springboot profile
dockerfile FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD test-push-service--SNAPSHOT.jar app. ...
- redis详解(四)-- 高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
- re(正则)模块
import re # re 存在5种使用方式 #1. macth #2.search #3.findall #4.split #5 sub re.match('^chen', 'chenhua123 ...
- c++11新特性总结(转)
1.类型与变量相关 1.1.nullptr: 取代了NULL,专用于空指针 1.2.constexpr: 近似const, 可以修饰变量,也可以修饰函数, 修饰变量如: const int globa ...
- 安装oracle后java -version命令显示 jdk version "1.3.1"的原因
因为先装的JDK,后装了oracle,oracle的JDK配置把原来的jdk路径替换掉了. 我的电脑->属性->高级->环境变量->系统变量->PATH ,把JDK的路径 ...
- 趣味编程:CPS风格代码(C#,F#版)
CPS风格代码(C#版) using System; namespace fp { class CPS { static int add(int x, int y) => x + y; stat ...
- servlet的的生命周期和使用
1 Servlet的生命周期是通过Servlet接口中的init(),service(),和destroy()方法来表示的,即Servlet从创建到销毁的过程.包括如何加载和实例化,初始化,处理请求, ...