UVA 12563 Jin Ge Jin Qu

3922 ワード

(If you smile when you see the title,this problem is foryou^^
For those who don't know KTV、see:http://en.wikipedia.org/wiki/Karaoke_ボックス
The e e e is one very popur song caled Jin Ge Jin Qu.It is a mix of 37 songs,and is
extremely long(11 minutes and 18 seconds)—I know that there re Jin Ge Jin Qu II,and
some other unoffical versions.But in this problem please forget about them.
Why is it popurSuppose you have only 15 seconds left(until your time is up)、then you shound
select another song as son as possible,because the KTV not crudiely stop a song before it ends
(people will get fustrated if it does so!)If you select a 2-minute song,you actually get 105 extra
secondsand if you select Jin Ge Jin Qu,you’ll get 663 extra seconds!
Now that you still have some time,but you’d like to make a plan now.You shoud stick to the
followwing rules:
•Don’t sing a song more than onece.
•For each song of length t,eigther sing it for exactlyt seconds,or don’t sing it at all.
•When a song is finished、always immediately start a new song.
Your goal is simple:sing as many songs as possible、and leave KTV as late as possible
have rule 3、this also maximizes the total lengths of all song we sing)when there are ties.
Input
The first line contains the number of test cases T(T≦100).Each test case begins with two positive
integers n,t(1≦n≦50,1≦t≦109
)、the number of candidate songs(BESIDES Jin Ge Jin Qu)
and the time left.The next line contains n positive integers,the lengths of each song,in
seconds.Each length will be less than 3 minutes—I know that most songs are longer than 3 minutes.
But don’t forget that we could manaualy“cut”the song afterwe feel satis fied、before the song ends.So
here「length」actually means「length of the part that we want to sing」。
It is garanted that the sum of lengths of all songs(including Jin Ge Jin Qu)will be strictly larger
than t.
Output
For each test case,print the maximumber of songs,and the total lengths
of songs that you'll sing.
Explanion:
In the first example、the best we can do is to sing the third song(80 seconds)、then Jin Ge Jin Qu
for another 678 seconds.
In the second example,we sing the first two(30+69=99 seconds).The n we still have one second
left,so we can sing Jin Ge Jin Qu for extra 678 seconds.However,if we sing the first and third song
instead(30+70=100 seconds)、the time is already up(since we only have 100 seconds in total)、so we
can’t sing Jin Qu anymore!
Sample Input
2
3 100
60 70,80
3 100
30 69
Sample Output
Case 1:2 758
Case 2:3 777
典型的な01リュックサックの問題は境界条件の処理に注意して、初期化dp配列は-1で、dp[0]=0で、さもなくば間違いが発生しやすいです。
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 10000
using namespace std;
int a[55];
int dp[maxn];
int main()
{
//    int n,t;
//    while(~scanf("%d%d",&n,&t))
//    {
//        for(int i=0;i<n;i++)
//            cin>>a[i];
//        memset(dp,-1,sizeof(dp));
//        for(int i=0; i<n; i++)
//            for(int j=a[i];j<t;j++)
//                if(~dp[j-a[i])
//                dp[i][j]=maxn{d[i][j],d[i+1][]}
//    }
      int T,n,t;
      cin>>T;
      int tmp=T;
      while(T--)
      {
          cin>>n>>t;
          memset(dp,-1,sizeof(dp));
          dp[0]=0;
          for(int i=0;i<n;i++)
          {
              cin>>a[i];
              for(int j=t-1;j>=a[i];j--)
                dp[j]=max(dp[j],dp[j-a[i]]+1);
          }
          int ans=0,index=0;
          for(int j=t-1;j>=0;j--)
          {
              if(dp[j]>ans)
              {
                  ans=dp[j];
                  index=j;
              }
          }
          printf("Case %d: %d %d
",tmp-T,ans+1,index+678); } }