题意:给出n首歌及每首歌的播放时间,然后在t秒内唱这些歌,最后的剩余时间要大于0;
思路:01背包问题,对于没次选择,只有取或不取两种状态,只需在这两种状态中找到最优的策略即可。
code:
#include <bits/stdc++.h>
using namespace std;
const int N=11;
const int M=10005;
#define ft(i,s,t) for (int i=s;i<=t;i++)
#define frt(i,t,s) for (int i=t;i>=s;i--)
int dp[M];
int main()
{
int n,m,T,t;
scanf("%d",&T);
ft(ca,1,T)
{
scanf("%d %d",&n,&m);
ft(i,1,m) dp[i]=-1;dp[0]=0;
ft(i,1,n)
{
scanf("%d",&t);
frt(j,m-t,0) if (dp[j]!=-1) dp[j+t]=max(dp[j]+1,dp[j+t]);
}
int a1=0,a2=0;
frt(i,m-1,0) if (dp[i]>a1) a1=dp[i],a2=i;
printf("Case %d: %d %d\n",ca,a1+1,a2+678);
}
}