当前位置:   article > 正文

【比赛】线性DP好题_给出n个柱子,每个高度为a[i],拔高1高 度花费为b[i]

给出n个柱子,每个高度为a[i],拔高1高 度花费为b[i]

A.「Codeforces 1324E」倒时差

思路:这题我想成贪心了:判断每次节点能否落在[l,r]区间内,时间复杂度 O(H*H)
然而为了达到这个状态,就必须作出决策,从而影响到后面的决策(虽然作出的决策不是唯一的)。当时考试时就想得太简单了。由于当前决策没有绝对的最优解,且每次DP只与前一个最优状态有关(最优子结构),所以正解是DP。
dp[i][j] 表示i次睡眠最终到 位置j的ans值,则:

dp[i][j]=max(dp[i-1][(j-a[i]+h)%h]+f,dp[i-1][(j-a[i]+1+h)%h]+f),其中f表示j是否在[l,r]区间内。

//这个代码虽然AC了,但有一点想不通:0和24怎么处理? 
#include<cstdio> 
#include<algorithm>
#include<cstring>
using namespace std; 
const int maxn=2005;
int n,h,l,r,a[maxn],dp[maxn][maxn],ans;
int main() {
    //freopen("sleep.in","r",stdin);
    //frepen("sleeep.out","w",stdout);
	memset(dp,-0x3f3f3f3f,sizeof(dp));
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
	}
	dp[0][0]=0;
	for(int i=1;i<=n;i++) {
		for(int j=0;j<=h;j++) { 
			bool f=0;
			if(j>=l&&j<=r) f=1;
			dp[i][j]=max(dp[i-1][(j-a[i]+h)%h]+f,dp[i-1][(j-a[i]+1+h)%h]+f);
		} 
	}
	for(int i=0;i<=h;i++) ans=max(ans,dp[n][i]);
	printf("%d",ans);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

B.4的倍数

思路:这是一道数学题:100%4==0, 计算以i结尾的子序列个数
没开longlong,就只有50pts

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=3*1e5+5;
char s[maxn];
ll n,ans,a[maxn];
int main() {
	//freopen("four.in","r",stdin);
	//freopen("four.out","w",stdout);
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) a[i]=s[i]-'0';
	for(int i=1;i<=n;i++) {
		if(a[i]%4==0) ans++;
		if((a[i-1]*10+a[i])%4==0) ans+=i-1;
	}
	printf("%lld",ans);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

C.「Codeforces 1207C」管道建设

思路 :
dp[i][0]表示前i条道路中第i+1条柱子为低柱的最小费用
dp[i][1]表示前i条道路中第i+1条柱子为高柱的最小费用
值为-1表示不合法,若前面的值为-1则不转移(防止ans太大,被错误调用)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<climits>
#define ll long long
using namespace std;
const int maxn=2*1e5+5;
ll dp[maxn][2],n,a,b;
char s[maxn];
int main() {
  //freopen("pipe.in","r",stdin);
  //freopen("pipe.out","w",stdout);
  int t;
  scanf("%d",&t);
  while(t--) {
  	scanf("%lld%lld%lld",&n,&a,&b);
  	scanf("%s",s+1);
  	memset(dp,-1,sizeof(dp));
  	dp[0][0]=b;
  	for(int i=1;i<=n;i++) {
  		if(s[i]=='1') {
  			dp[i][1]=dp[i-1][1]+a+2*b;
  		}
  		else {
  			if(i==1) {
  				dp[i][1]=dp[i-1][0]+2*a+2*b;
  				dp[i][0]=dp[i-1][0]+a+b;
  			}
  			else {
  				dp[i][1]=dp[i-1][1]+a+2*b;
  			    if(dp[i-1][0]!=-1) dp[i][1]=min(dp[i][1],dp[i-1][0]+2*a+2*b);
  			    dp[i][0]=dp[i-1][1]+2*a+b;
  			    if(dp[i-1][0]!=-1) dp[i][0]=min(dp[i][0],dp[i-1][0]+a+b);
  			}
  		}
  	}
  	printf("%lld\n",dp[n][0]);
  }
  return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号