USACO Raucous Rockers 解题报告

看USACO标程是动态规划的题,当然看题目也能感觉出来,但是没想到表达式。递归的思路中毒太深,这道题数据比较小,也过了所有测试点。

/* 
ID: thestor1 
LANG: C++ 
TASK: rockers 
*/
#include <iostream>  
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <climits>  
#include <cassert>  
#include <string>  
#include <vector>  
#include <set>  
#include <queue>  
#include <stack>  
#include <algorithm>  
  
using namespace std;

const int MAXN = 20;
int songs[MAXN];

void iter(int n, int t, int m, const int N, const int T, const int M, int c, int &cnt)
{
	if(n >= N)
	{
		if(c > cnt)
		{
			cnt = c;
		}
		return;
	}

	iter(n + 1, t, m, N, T, M, c, cnt);

	if(t + songs[n] <= T)
	{
		iter(n + 1, t + songs[n], m, N, T, M, c + 1, cnt);
	}
	else if(songs[n] <= T && m + 1 <= M)
	{
		iter(n + 1, songs[n], m + 1, N, T, M, c + 1, cnt);
	}
}

int main()  
{
	FILE *fin  = fopen ("rockers.in", "r");
	FILE *fout = fopen ("rockers.out", "w");
	
	int N, T, M;
	fscanf(fin, "%d %d %d", &N, &T, &M);
	for(int i = 0; i < N; ++i)
	{
		fscanf(fin, "%d", &songs[i]);
	}

	int cnt = 0;
	iter(0, 0, 1, N, T, M, 0, cnt);
	fprintf(fout, "%d\n", cnt);
	return 0;  
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值