Codeforces 607A Chain Reaction【dp】

来源:互联网 发布:ios编程 第四版 微盘 编辑:程序博客网 时间:2024/06/05 23:04

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. Thei-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 distancebi 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 andbi (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, soai ≠ aj ifi ≠ j.

Output

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Examples
Input
41 93 16 17 4
Output
1
Input
71 12 13 14 15 16 17 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 position 1337 with power level 42.


题目大意:

给你N个灯塔,接下来N行,每行两个元素,表示灯塔的位子和能量范围。当一个灯塔建立的时候,其左边能量范围内的灯塔,将被摧毁,如果此灯塔没有建立的时候,其没有作用,此时我们可以在最初的时候,所有灯塔的最右边建立一个灯塔,位子和能量范围是任意设定的。问最少有多少灯塔被摧毁。


思路:


1、考虑dp,如果我们考虑dp最少摧毁的数量,显然比较困难,考虑逆向思维,我们设定dp【i】表示从0-i位子中最多建立的灯塔数,那么其区间【0-i】最少摧毁的灯塔数即已知。


2、考虑其状态转移方程(其中a【i】=x表示位子i上灯塔的作用范围为x):

①如果此灯塔是第一个灯塔,那么dp【i】=1,表示这个灯塔左侧没有灯塔能被其摧毁。

②如果此灯塔的作用范围能够覆盖到第一个灯塔,那么dp【i】=1,表示如果建立此灯塔,那么其左侧全部灯塔都被摧毁了,那么区间【0-i】内,只能建立当前一个灯塔。

③如果以上两条都不符合,那么dp【i】=1+dp【i-a【i】-1】表示,当前灯塔建立(1),并且累加上其作用范围以外的区间【0,i-a【i】-1】最多的灯塔数(dp【i-a【i】-1】)。

④如果当前位子i没有灯塔,那么dp【i】=dp【i-1】。


3、初始建立一个灯塔在所有灯塔的最右侧其位子和作用范围任意,其实就是在说,建立完这个灯塔之后,其最右端的一些灯塔就被摧毁了,那么我们最多建立的灯塔数,就是相当于,在从【0,i】取一个最大值(0<=i<=1000000),那么最小摧毁数=n-最大建立数。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[1000040];int dp[1000040];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            int pos,r;            scanf("%d%d",&pos,&r);            a[pos]=r;        }        int f=0,posf;        for(int i=0;i<=1000000;i++)        {            if(a[i]==0)dp[i]=dp[i-1];            else            {                if(f==0)dp[i]=1,f++,posf=i;                else if(i-posf<=a[i])dp[i]=1;                else dp[i]=dp[i-a[i]-1]+1;            }        }        int output=0;        for(int i=0;i<=1000000;i++)        {            output=max(output,dp[i]);        }        printf("%d\n",n-output);    }}



0 0
原创粉丝点击