一 原题
You just inherited the rights to N (1 <= N <= 20) previously unreleased songs recorded by the popular group Raucous Rockers. You plan to release a set of M (1 <= M <= 20) compact disks with a selection of these songs. Each disk can hold a maximum of T (1 <= T <= 20) minutes of music, and a song can not overlap from one disk to another.
Since you are a classical music fan and have no way to judge the artistic merits of these songs, you decide on the following criteria for making the selection:
- The songs on the set of disks must appear in the order of the dates that they were written.
- The total number of songs included will be maximized.
PROGRAM NAME: rockers
INPUT FORMAT
Line 1: | Three integers: N, T, and M. |
Line 2: | N integers that are the lengths of the songs ordered by the date they were written. |
SAMPLE INPUT (file rockers.in)
4 5 2 4 3 4 2
OUTPUT FORMAT
A single line with an integer that is the number of songs that will fit on M disks.SAMPLE OUTPUT (file rockers.out)
3
二 分析
数据范围贼小,直接dfs枚举
三 代码
运行结果:
USER: Qi Shen [maxkibb3] TASK: rockers LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4184 KB] Test 2: TEST OK [0.000 secs, 4184 KB] Test 3: TEST OK [0.000 secs, 4184 KB] Test 4: TEST OK [0.000 secs, 4184 KB] Test 5: TEST OK [0.000 secs, 4184 KB] Test 6: TEST OK [0.000 secs, 4184 KB] Test 7: TEST OK [0.000 secs, 4184 KB] Test 8: TEST OK [0.097 secs, 4184 KB] Test 9: TEST OK [0.011 secs, 4184 KB] Test 10: TEST OK [0.000 secs, 4184 KB] Test 11: TEST OK [0.032 secs, 4184 KB] Test 12: TEST OK [0.205 secs, 4184 KB] All tests OK.
Your program ('rockers') produced all correct answers! This is your submission #2 for this problem. Congratulations!
AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:rockers
*/
#include<cstdio>
int n, t, m;
int songs[25];
int ans;
void dfs(int s_id, int d_id, int left_size, int num) {
if(d_id == m || s_id == n) {
if(num > ans) ans = num;
return;
}
if(ans == n) return;
dfs(s_id + 1, d_id, left_size, num);
if(left_size >= songs[s_id]) dfs(s_id + 1, d_id, left_size - songs[s_id], num + 1);
if(songs[s_id] <= t && d_id < m - 1) dfs(s_id + 1, d_id + 1, t - songs[s_id], num + 1);
}
int main() {
freopen("rockers.in", "r", stdin);
freopen("rockers.out", "w", stdout);
scanf("%d%d%d", &n, &t, &m);
for(int i = 0; i < n; i++) scanf("%d", &songs[i]);
dfs(0, 0, t, 0);
printf("%d\n", ans);
}