简单的烦恼
作者:互联网
链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
网易云音乐推出了定时关闭播放的功能,假设到了定时关闭播放的时间,当前这首歌还没有播放完,那就把它播放完关闭;如果到了定时关闭的时间,当前歌恰好播放完,那就立即关闭。xrc 在知道网易云这个算法后,想知道如果自己定时 t 时间后关闭播放,那最多能听多长时间的歌,已知 xrc 歌单中一共有 n 首歌,并且知道每首歌的播放时间分别是 a[i]。
输入描述:
第一行一个整数T(T <= 23),表示数据组数。 在每组输入数据中,第一行有两个正整数,n(n <= 200), t(t <= 80000),分别表示歌单中歌曲的数目,和题目描述中的t。 第二行中有n个正整数a[i](a[i] <= 400),表示每首歌曲的时间长度。
输出描述:
对于每组数据,输出一个ans,表示最多能听多长时间的歌曲。
示例1
输入
1 3 7 4 3 2
输出
9
说明
先听第2首歌和第3首歌,最后播放第1首歌,在7单位时间后,第3首歌还没有播放完,所以要等第1首歌播放完,共能听9单位时间的歌。
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<bitset>
#include<cctype>
#include<queue>
using namespace std;
int t, n, m, a[1000], ans, dp[100000], x;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
ans = 0;
memset(dp, 0, sizeof(dp));
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
x = m;
for (int i = 0; i < n - 1; i++) {
for (int j = m - 1; j >= a[i]; j--) {
dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
}
}
ans = dp[m - 1] + a[n - 1];
cout << ans << endl;
}
return 0;
}
标签:include,int,首歌,烦恼,cin,简单,播放,dp 来源: https://blog.csdn.net/TUA_4ever/article/details/122025084