Title Description:
Choose a certain number of songs from the N song, each song needs a certain time to sing:
1, the requirement is not more than the time st-1 conditions, the number of selected the most;
2, the second in the same number of cases, the song time required to maximize the total.
Problem Solving Ideas:
Essence 0, 1 knapsack problem, the time of each song as the volume of goods, value assigned to 1, backpack capacity of st-1
How to solve the greatest number of songs on the basis of the total time sum?
Record updates in the process of moving the rules:
1. The current state is more than the number that has been asked, the maximum number of updates and the longest elapsed time
2, the current state and the number of the request, the maximum time to update
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int d[10010];int time [60 ];int N,st;int Main () { int T; scanf ("%d", &t); for (int t=1; t<=t; ++t) { scanf ("%d%d", &n,&st); for (int i=1; i<=n; ++i) scanf ("%d", &time[i]); int ans=0; int maxx=0; Memset (d,0,sizeof (d)); for (int i=n; i>=1;-I.) for (int j=st-1; j>=time[i];--j) { if (j==time[i]| | D[J-TIME[I]]>0)//whether to add the current item { D[j]=max (d[j],d[j-time[i]]+1); if (d[j]>maxx| | (D[j]==maxx&&j>ans)) Maximum volume, maximum value status update { ans=j; MAXX=D[J];}} } printf ("Case%d:%d%d\n", t,maxx+1,ans+678); } return 0;}
Harvest:
Record certain states and update them during dynamic planning
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
uva12563 Jin Ge jin Qu hao