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 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 thei-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.

Sample test(s)
input
4
1 9
3 1
6 1
7 4
output
1
input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
output
3
Note

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 动态规划的更多相关文章

  1. Codeforces 837D 动态规划

    Codeforces 837D 动态规划 传送门:https://codeforces.com/contest/837/problem/D 题意: 给你n个数,问你从这n个数中取出k个数,这k个数的乘 ...

  2. codeforces 1183H 动态规划

    codeforces 1183H 动态规划 传送门:https://codeforces.com/contest/1183/problem/H 题意: 给你一串长度为n的字符串,你需要寻找出他的最长的 ...

  3. Codeforces 607A - Chain Reaction - [DP+二分]

    题目链接:https://codeforces.com/problemset/problem/607/A 题意: 有 $n$ 个塔排成一行,第 $i$ 个激光塔的位置为 $a_i$,伤害范围是 $b_ ...

  4. Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心

    After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...

  5. CODEFORCES 429B 动态规划

    http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...

  6. CodeForces 366C 动态规划 转化背包思想

    这道题目昨晚比赛没做出来,昨晚隐约觉得就是个动态规划,但是没想到怎么DP,今天想了一下,突然有个点子,即局部最优子结构为 1-j,j<i,遍历i,每次从所有的1到j当中的最优解里面与当前商品进行 ...

  7. Comb CodeForces - 46E (动态规划)

    题面 Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To h ...

  8. 2018省赛赛第一次训练题解和ac代码

    第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces ...

  9. Codeforces Flipping game 动态规划基础

    题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...

随机推荐

  1. django-类装饰器method_decorator

    import os from django.shortcuts import render from django.contrib.admin.views.decorators import staf ...

  2. [Flutter] 支持描边效果的Text

    新版的flutter已经自带这个功能了.TextSyle 中一个shadow . 目前flutter中没找到很好的办法给Text增加描边.自己扩展了一个TextEx,可以实现简单的描边效果,能满足大部 ...

  3. leetcode459

    public class Solution { public bool RepeatedSubstringPattern(string s) { var len = s.Length; ) { ret ...

  4. tab form

    procedure TForm2.ToolButton1Click(Sender: TObject); var frm: TForm; begin frm := TForm.Create(Applic ...

  5. php解析url并得到url中的参数

    <?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&ar ...

  6. git-采集编码搜索

    https://github.com/search?utf8=%E2%9C%93&q=%E9%87%87%E9%9B%86%E7%BC%96%E7%A0%81&type= https: ...

  7. windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息<转>

    GetsysInfo.h: #ifndef _H_GETSYSINFO #define _H_GETSYSINFO #pragma once #include <afxtempl.h> c ...

  8. Gulp的安装与配置

    http://blog.csdn.net/itlsx/article/details/49981459

  9. layoutSubviews相关总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ------- - (void)layoutSubview ...

  10. php 时间操作归类

    对于php时间表示有两种: 一.‘xxxx-xx-xx'这种容易分辨的格式 二.unix时间戳格式 他们的之间的转换关系是: 常规格式转时间戳 $T='2014-05-24'; $Tr=strtoti ...