zoukankan      html  css  js  c++  java
  • 洛谷P2736 [USACO3.4]“破锣摇滚”乐队 Raucous Rockers

    题目

    https://www.luogu.com.cn/problem/P2736

    思路

    一道简单的DP题,注意到状态的转移比较复杂,推荐使用记忆化搜索。注意递归返回条件的优先级顺序(我写挂了2次TAT)。

    代码

    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int dp[21][21][21],n,m,t,a[21];
    int dfs(int step,int remain,int id){
    	int r1=0,r2=0;
    	if(step>n||id>m) return 0;
    	if(dp[step][remain][id]!=-1) return dp[step][remain][id];
    	if(a[step]>t) return dfs(step+1,remain,id);
    	if(a[step]>remain){
    		r1=dfs(step+1,remain,id);
    		if(id<m) r2=dfs(step+1,t-a[step],id+1)+1;
    	}
    	else{
    		r1=dfs(step+1,remain,id);
    		r2=dfs(step+1,remain-a[step],id)+1;
    	}
    	dp[step][remain][id]=max(r1,r2);
    	return dp[step][remain][id];
    }
    int main(){
    	int i,j,k,ans;
    	scanf("%d%d%d",&n,&t,&m);
    	for(i=1;i<=n;i++)
    		scanf("%d",&a[i]);
    	for(i=1;i<=n;i++)
    		for(j=0;j<=20;j++)
    			for(k=1;k<=m;k++)
    				dp[i][j][k]=-1;
    	ans=dfs(1,t,1);
    	printf("%d",ans);
    	//system("pause");
    	return 0;
    }
  • 相关阅读:
    code3728 联合权值
    Codevs 4600 [NOI2015]程序自动分析
    code1540 银河英雄传说
    code1074 食物链
    堆排序
    哈夫曼树与哈夫曼码
    优先队列用法
    code1154 能量项链
    code1225 八数码Bfs
    javascript5
  • 原文地址:https://www.cnblogs.com/landmine-sweeper/p/14023283.html
Copyright © 2011-2022 走看看