关于暴力 DP 写挂
  • 板块P1484 种树
  • 楼主Cht_master
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/11/6 17:49
  • 上次更新2023/11/4 01:15:33
查看原帖
关于暴力 DP 写挂
261046
Cht_master楼主2021/11/6 17:49

Rt,比答案小

//f(i,j):前i棵树中种了j棵的最大值.
//f(i,j)=max(f(i-1,j),f(i-2,j-1)+a(i)).
#include<bits/stdc++.h>
using namespace std;
const int mxN(6e3),INF(0x3f3f3f3f);
int n,k;
int a[mxN+5],f[mxN+5][mxN+5],ans;
int main(){
//	freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&k);
	for(int i(1);i<=n;++i)scanf("%d",&a[i]);
	memset(f,-0x3f,sizeof(f));
	f[1][0]=0,f[1][1]=a[1],ans=max(0,a[1]);
	for(int i(2);i<=n;++i)
		for(int j(0);j<=k;++j)
			f[i][j]=max(f[i-1][j],(j-1>=1)?(f[i-2][j-1]+a[i]):(-INF)),ans=max(ans,f[i][j]);
	printf("%d",ans);
	return 0;
}
2021/11/6 17:49
加载中...