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 < ...
随机推荐
- 慕课网价值149《前端JavaScript面试技巧》笔记大公开——适应群体(学生或应届毕业生)
1.基础知识(一) http://note.youdao.com/noteshare?id=b81f56399b01da0ab5e870ea612ab94b&sub=B8ECBC1B57154 ...
- WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException
Hadoop 2.7.4 The reason is this: originally, DataStreamer::closeResponder always prints a warning ab ...
- Java工具类实现校验公民身份证的有效性
转自:https://www.oschina.net/code/snippet_1859292_39120 1 package com.tg.user.controller; import java. ...
- UI5-文档-2.2-使用SAP Web IDE开发应用程序
SAP Web IDE是一种基于Web的开发环境,它是为使用最新的创新开发SAPUI5复杂的应用程序.开发和扩展SAP Fiori应用程序.开发移动混合应用程序以及使用插件和模板扩展SAP Web I ...
- display:inline、block、inline-block区别
display:inline.block.inline-block区别 display:block就是将元素显示为块级元素. display:inline就是将元素显示为行内元素. inline-bl ...
- Linux 的伪终端的基本原理 及其在远程登录(SSH,telnet等)中的应用
本文介绍了linux中伪终端的创建,介绍了终端的回显.行缓存.控制字符等特性,并在此基础上解释和模拟了telnet.SSH开启远程会话的过程. 一.轻量级远程登录 之前制作的一块嵌入式板子,安装了嵌入 ...
- jsp页面转发获取不到参数
使用的是<input type="hidden" name="nameid" value="${nameid}"/>,隐藏默认值 ...
- Python环境配置, atom-python配置
环境变量 路径 在window下配置环境变量,配置到文件夹级就可以了: D:\program\python3 编码 因为windows默认的编码是ASIIC,所以使用atom时候中文是乱码,需要在环境 ...
- 使用github的流程
使用github的流程 在实际项目开发中,按照如下步骤使用git进行代码管理 1.项目经理在开发之初,创建好仓库,上传项目的框架.组员分支 2.组员克隆项目框架,同步分支,按分工开发,在分支提交代码 ...
- hadoop 链接 mysql
数据在hadoop服务器上,需要导入mysql 可以直接在hadoop上操作 要知道 mysql的 账号 密码 host 步骤: 进入shell 输入: mysql -u 'name' -p'pas ...