UVa 12563 Jin Song Rujia second edition example 9-5;

Source: Internet
Author: User
Tags cas

Problem Jjin Ge Jin Qu [H]ao

(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_box

There is one very popular song called Jin Ge Jin Qu (Jin song). It is a mix of PNS songs, and is extremely long (minutes and seconds) [1].

Why is it popular? Suppose you has only seconds left (until your time are up) and then you should select another song as soon as possible, be Cause 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 extra seconds! .... 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 the following rules:

    • Don ' t sing a song more than once (including Jin Ge Jin Qu).
    • For each song of the length 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 we had rule 3, this also MA Ximizes the total lengths of any songs we sing) when there is ties.


The first line contains the number of test Cases T(T<=100). Each test case begins with the positive integers 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, in seconds. Each length would be less than 3 minutes[2]. It is guaranteed, the sum of lengths of all songs (including Jin Ge Jin Qu) would be strictly larger than T.


For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths of songs, which you ' L L sing.

Sample Input
23 10060 70 803 10030 69 70
Output for the Sample Input
Case 1:2 758Case 2:3 777

Test instructions

1, the more songs in time t sing the better;
2, also requires the total time to sing the longer the better;
At least one second to sing Jin Ge Jin qu, because must choose to sing Jin ge Jin qu to the best;
So use t-1 time to choose to sing the n song;

As much as possible to sing, in the same amount of time as long as possible;

Idea: Take the time as the cost, quantity as value, Carry on 01 knapsack, because the reason of the recursion causes can not confirm the quantity maximum, the time is how much, so to initialize the DP to judge by a special value, namely the complete backpack;
In this way, the time of the greatest number of times is guaranteed to be the total time spent;

Links: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34887


#include<cstdio> #include<cstring> #include<algorithm> #include<iomanip> #include<string> #include<cmath> #include<vector> #include<queue> using namespace std; int dp[10000]; const int inf = 0x3f3f3f3f; int next[55]; int a[55], t, n; int main() {      int T, cas=1;      cin>>T;      while (T--)      {          scanf( "%d%d" ,&n,&t);          for (int i = 1; i <= n; i++){              scanf( "%d" ,&a[i]);          }          for (int i = 1; i < 10000; i++) dp[i] = -inf;          dp[0] = 0;          for (int i = 1; i <= n; i++){              for (int j = t-1; j >= a[i]; j--){                  dp[j] = max(dp[j],dp[j-a[i]]+1);              }          }          int time ,cnt, mas = -inf, pos;          for (int i = t-1; i >= 0; i--){              if (dp[i]>mas){                  mas = dp[i]; pos = i;              }          }          printf( "Case %d: %d %d\n" ,cas++,mas+1,pos+678);//最后一秒用来唱jin ge jin qu;      }      return 0; }

UVa 12563 Jin Song Rujia second edition example 9-5;

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.