UVA 12563 劲歌金曲(滚动数组)

这道题练习了滚动数组,不过跟0-1背包问题有点不同,为了记录准确时间,通过设置条件把无用状态剔出来,消除了错误时间,f[i]表示i时,唱了多少首歌。

#include<bits/stdc++.h>
#define max1 9700
using namespace std;

int f[max1];
int ans[max1];
int main(){
int n;
scanf("%d",&n);
int mark=0;
int kase=1,time=0;
while(n--)
{
	
	memset(f,0,sizeof(f));
	memset(ans,0,sizeof(ans));
	int count,left,j,cishu=0;
	int time;
	scanf("%d %d",&count,&left);

	
	for(int i=0;i<count;i++){
	
	scanf("%d",&time);
	
	for( j=left-1;j>=time;j--)
	if(f[j - time] >= 1|| j == time){
		f[j]=max(f[j],f[j-time]+1);
		cishu=max(cishu,f[j]);
	} 
	
}
	for(j=left-1;cishu!=f[j];j--) ;

	if(cishu==0)
	printf("Case %d: %d %d\n",kase++,1,678);	
	else
	printf("Case %d: %d %d\n",kase++,cishu+1,j+678);	
}

	
}
发布了57 篇原创文章 · 获赞 58 · 访问量 664

猜你喜欢

转载自blog.csdn.net/weixin_43568895/article/details/103381128