607A Chain Reaction(DP)

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.

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 position 1337 with power level 42.

一堆培根,每個培根在數軸上一個位置a,擁有能量b,當激活這個培根的時候,它會將在它左面距離他b以內(包括b)的培根破壞,一旦培根遭到破壞,就無法激活,現在你要放第n+1個培根在給的n個培根的右面,能量自定,位置自定,但一定是最右面的一個,然後從右向左依次激活培根,問最少破壞幾個培根。

想了一下就是一個遞推,每個培根只有破壞和不破壞兩個狀態,然後最右面那個培根的作用就是控制右面連續幾個培根被破壞。
也就是說,這個培根壞了,左面轉移過來可能是好的也可能是壞的培根,但是這個培根如果是好的,那麼一定是從好的培根轉移過來。

dp[0][i]代表這個培根壞了,dp[1][i]代表這個培根是好的。
dp[0][i] = max(dp[0][i - 1],dp[1][i - 1]);
dp[1][i] = dp[1][i - b[i]] + 1;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int dp[2][1000005],leap[1000005];
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n;
    scanf("%d",&n);
    memset(leap,0,sizeof(leap));
    memset(dp,0,sizeof(dp));
    for(int i = 1;i <= n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        leap[a] = b;
    }
    if(leap[0])dp[0][0] = dp[1][0] = 1;
    else dp[0][0] = dp[1][0] = 0;
    for(int i = 1;i <= 1000000;i++)
    {
        if(leap[i])
        {
            if(i > leap[i])
            {
                dp[0][i] = max(dp[0][i - 1],dp[1][i - 1]);
                dp[1][i] = dp[1][i - leap[i] - 1] + 1;
            }
            else
            {
                dp[0][i] = max(dp[0][i - 1],dp[1][i - 1]);
                dp[1][i] = 1;
            }
        }
        else
        {
            dp[0][i] = max(dp[0][i - 1],dp[1][i - 1]);
            dp[1][i] = dp[1][i - 1];
        }
    }
    int ans = 0;
    for(int i = 0;i <= 1000000;i++)
        ans = max(ans,max(dp[0][i],dp[1][i]));
    printf("%d",n - ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章