12563-jin Ge Jin Qu HAO--[DP recursion]

Source: Internet
Author: User

(If you smiled to see the title, this problem are for you ^_^) For those who don ' t know KTV, See:http://en.wikipedia.org/wiki/karaoke_boxthere is one very popular song called Jin Ge Ji n Qu (). It is a mix of PNS songs, and is Extremelylong (one minutes and seconds)-I know that there am Jin Ge Jin Qu II and III , and some otherunofficial versions.  Problem forget about them. Why is it popular? Suppose you has only seconds left (until your time are up) and then you shouldselect another song as soon as possible, Bec Ause the KTV would not crudely stop a song before it ends (people would get frustrated if it does so!). If you select a 2-minute song, you actually get extraseconds!  .... And if you select Jin Ge jin Qu, you ' ll get 663 extra seconds!!! Now so you still has some time, but you're ' d like to make a plan now. You should stick to thefollowing rules: don ' t sing a song more than once (including Jin Ge Jin Qu). for each song of Len Gth T, either sing it for Exactly t seconds, or don ' t sing it at All. when a song was finished, always immediately start a new song. Your goal is simple:sing as many songs as possible, and leave KTV as late as possible (since Wehave rule 3, this also max Imizes the total lengths of any songs we sing) when there is ties.InputThe first line contains the number of test cases T (t≤100).  Each test case is begins with the positiveintegers N, t (1≤n≤50, 1≤t≤109), the number of candidate songs (besides Jin Ge Jin Qu) and the time left (in seconds). The next line contains n positive integers, the lengths of each song, Inseconds. Each length would be less than 3 minutes-i know the most songs is longer than 3 minutes. But don ' t forget this we could manually "cut" the song after we feel Satisfied, before the song ends. So here's "length" actually means "length of the part, the We want to sing". It is guaranteed, the sum of lengths of all songs (including Jin Ge Jin Qu) would be strictly Largerthan T.OutputFor each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengthsof songs so you ll Sing.Explanation:In the first example, the best we can do are to sing the third song (z seconds), then Jin Ge Jin qufor another 678 seconds  . In the second example, we sing the first (30+69=99 seconds). Then we still has one secondleft, so we can sing Jin Ge jin Qu for extra 678 seconds. However, if we sing the first and third songinstead (30+70=100 seconds), the time is already up (since we are only having an SE Conds in total), so Wecan ' t sing Jin Ge jin Qu anymore!Sample Input23 10060 70 803 10030 69 70Sample OutputCase 1:2 758Case 2:3 777

Topic Analysis:

Each song is selected at most, by the condition 180n+678>t the maximum t=9678s, can be converted to 0-1 knapsack problem:

1. State D[i][j] indicates that the maximum number of songs can be selected from the I,i+1,...,n in the case where the current time remaining is J.

State transfer equation: d[i][j]=max{d[i+1][j], d[i+1][j-t[i]]+1}, (j-t[i]>0), where D[i+1][j] represents the maximum number of songs selected when the song is not selected, d[i+1][j-t[i]]+ 1 indicates the maximum number of songs selected after the song is selected. Note that when j-t[i]<=0, that is, the remaining time is not more than 0 o'clock, the first song can not be selected, at this time d[i][j]=d[i+1][j];

The boundary condition is:i>n,d[i][j]=0;

2. As the title requires the maximum number of songs to ensure that the longest time to sing, then the same can be converted to 0-1 knapsack problem, but D[i][j] to calculate first:

State Song[i][j] Indicates the maximum amount of time to be selected from the I,i+1,...,n in the case of the current time remaining J.

State transfer follows D[I][J]: Make V1=d[i+1][j] (that is, do not select the first song), V2=d[i+1][j-t[i]]+1 (choose the first song)

If:

1) V2>V1, note that the first song must point, Song[i][j]=song[i+1][j-t[i]]+t[i];

2) V2==v1, Song[i][j]=max{song[i+1][j],song[i+1][j-t[i]]+t[i]};

3) V2<v1, stating that the first song must not point, song[i][j]=song[i+1][j];

Reverse recursion, the answer is d[1][t] and song[1][t].

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intNinf=-100000000;6 Const intmaxn= -;7 Const intmaxt=10000;8 intt[maxn+5];9 intd[maxn+5][maxt];Ten intsong[maxn+5][maxt]; One intn,t; A  - voidsolve () { -      for(intj=t;j>=0; j--) {//calculate song[i][j] boundary the         if(j-t[n]>0) song[n][j]=T[n]; -         Elsesong[n][j]=0; -     } -      for(inti=n;i>=1; i--){ +          for(intJ=t;j>0; j--){ -             intV1,v2; +v1=d[i+1][J];//The boundary condition is implied in it, when I==n, d[i+1][]=0; A             if(j-t[i]<=0) V2=ninf;//if j-t[i]<=0, let the state v2 equals negative infinity, must be less than V1 at             Elsev2=d[i+1][j-t[i]]+1; -d[i][j]=Max (V1,V2); -             //Update Song -             if(v2>v1) { -song[i][j]=song[i+1][j-t[i]]+T[i]; -             } in             Else if(v2==v1) { -Song[i][j]=max (song[i+1][j],song[i+1][j-t[i]]+t[i]); to             } +             Elsesong[i][j]=song[i+1][j]; -         } the     } * } $ intMainintargcConst Char*argv[]) {Panax Notoginseng     intKase; -scanf"%d",&Kase); the      for(inttt=1; tt<=kase;tt++){ +scanf"%d%d",&n,&T); Amemset (T,0,sizeoft); the          for(intI=1; i<=n;i++) +scanf"%d",&t[i]); -memset (D,0,sizeofd); $Memset (song,0,sizeofsong); $ solve (); -         intnum=d[1][t]+1; -         intlen=song[1][t]+678; the          -printf"Case %d:%d%d\n", Tt,num,len);Wuyi     } the     return 0; -}

   

12563-jin Ge Jin Qu HAO--[DP recursion]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.