看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;
}