zoukankan      html  css  js  c++  java
  • Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)

    C. 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 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 position1337 with power level 42.



    题目链接:点击打开链接

    给出n个点的坐标以及照耀范围, 现要在最右边放置一个灯塔, 位置和照耀范围随意, 问最少有几个灯塔能够被摧毁.

    dp数组记录第i个灯塔存在时照亮的数量, 求出每一个灯塔摧毁范围同一时候更新dp和记录最大的dp, 范围大于0则等于那个点照亮数量围, 且自

    己伤害范围不为0时, 再把自己算进去.

    AC代码:

    #include "iostream"
    #include "cstdio"
    #include "cstring"
    #include "algorithm"
    #include "queue"
    #include "stack"
    #include "cmath"
    #include "utility"
    #include "map"
    #include "set"
    #include "vector"
    #include "list"
    #include "string"
    #include "cstdlib"
    using namespace std;
    typedef long long ll;
    const int MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    const int MAXN = 1e6 + 6;
    int n, dp[MAXN], magic[MAXN], ans;
    int main(int argc, char const *argv[])
    {
    	scanf("%d", &n);
    	for(int i = 0; i < n; ++i) {
    		int x, y;
    		scanf("%d%d", &x, &y);
    		magic[x] = y;
    	}
    	for(int i = 0; i <= 1e6; ++i) {
    		int x = i - magic[i] - 1;
    		if(x < 0) dp[i] = 0;
    		else dp[i] = dp[x];
    		if(magic[i]) dp[i]++;
    		if(dp[i] > ans) ans = dp[i];
    	}
    	printf("%d
    ", n - ans);
    	return 0;
    }

     

  • 相关阅读:
    java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较
    jdk8 stream实现sql单表select a,b,sum(),avg(),max() from group by a,b order by a,b limit M offset N及其性能
    postgresql cstore_fdw性能测试
    Oracle JDBC prefetch: how to avoid running out of RAM
    mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'
    kafka外部访问设置
    mysql 排序长度限制之max_length_for_sort_data以及mysql两种排序算法
    mybatis三个执行器的差别
    Dubbo的集群容错与负载均衡策略及自定义(一致性哈希路由的缺点及自定义)
    mysql中包含长字段索引的优化
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7142041.html
Copyright © 2011-2022 走看看