线性dp Codeforces Round #336 (Div. 2) C题 Chain Reaction

Chain Reaction

There are n beacons located at distinct positions on a number line. The i-th beacon has position a i and power level b i. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance b i 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.


题目大意:n 个塔,从右往左激活,每激活一个塔,这个塔将把它伤害范围内的塔全部摧毁(这个塔本身没有摧毁),然后在最右边再加一个塔(位置不定,伤害范围不定),问最少被摧毁的塔的数量;

如果从右往左思考,会发现没有什么明显确切的关系,换个思路,从左往右思考;

设 dp[i] 表示从右往左激活第 i 个位置(不是表示塔的下标,是位置)后剩余的塔;

那么转移方程为:

  1. 该位置没有塔,dp[i]=dp[i-1];
  2. 该位置有塔,但是该塔伤害范围大于 i ,dp[i]=1;
  3. 改位置有塔,伤害范围小于 i ,dp[i]=dp[i-p[i]-1]+1;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=1e9+7;
int a[N],b[N],p[10*N],vis[10*N],dp[10*N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vis[a[i]]=1,p[a[i]]=b[i];
	}
	int ans=0;
	for(int i=0;i<10*N;i++){
		if(!vis[i]) dp[i]=dp[i-1];
		else{
			if(p[i]>=i) dp[i]=1;
			else dp[i]=dp[i-p[i]-1]+1;
		}
		ans=max(ans,dp[i]);
	}
	cout<<n-ans<<endl;
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值