zoukankan      html  css  js  c++  java
  • Codeforces 607A 动态规划

    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 0001 ≤ 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[1000010],dp[1000010];
    int main()
    {
        int i,n,pos,MPos=0;
        int Max;
        scanf("%d",&n);
        memset(power,0,sizeof(power));
        for(i=0;i<n;i++)
        {
            scanf("%d",&pos);
            scanf("%d",&power[pos]);
            if(pos>MPos) MPos=pos;
        }
        memset(dp,0,sizeof(dp));
        if(power[0]>0) dp[0]=1;
        Max=dp[0];
        for(i=1; i<=MPos; i++)
        {
            if(power[i]==0) dp[i]=dp[i-1];
            else
            {
                if((i-power[i]-1)>=0)
                    dp[i]=dp[i-power[i]-1]+1;
                else dp[i]=1;
            }
            if(dp[i]>Max) Max=dp[i];
        }
        cout<<n-Max<<endl;
        return 0;
    }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    C# Cache的类方法
    a href="javascript:void(0)"
    细数银行7大手续费 这些冤枉钱别再交了
    mysql 中order by 与group by的顺序
    java springboot 将html页面转成图片
    SpringBoot读取Resource下文件的几种方式
    在CentOS 6.X中安装中文字体
    第三方api调用HttpClient
    java通过html生成pdf,支持css和图片以及横向打印
    nginx配置静态文件和服务代理
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5133214.html
Copyright © 2011-2022 走看看