线性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
#define LL long long
#define pa pair
#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;
}

你可能感兴趣的:(#,线性dp,动态规划)