UVA12563 Jin Ge jin Qu hao (DP, backpack + tips)

Source: Internet
Author: User

Test instructions: In a given time, the maximum number of songs can be sung, in the case of the most songs, so that the longest time to sing.

The question is similar to the 01 knapsack problem, can be used 01 knapsack problem solving ideas to beg, each song is equivalent to the item, the length of the song equals the weight of the item, each song "value" is 1. Because the Golden song is the longest time, so the last to stay at least 1 seconds to start singing Jin song, so calculate t-1 time to sing the most songs and time, the final answer for the song number plus 1, time plus Golden song time. Here I use a scrolling array to calculate this value and record t-1 with Len.

It is important to note that because the requirements are continuous singing, and the maximum number of songs required in the case of the longest, if the normal backpack storage, it is difficult to get the longest time, because F[len] only store the maximum number of songs, but do not know how much time these songs are sung. If the maximum number of songs is num, and the minimum time to sing num songs is tmin, then the array from f[tmin] to F[len] is equal to num, we don't know the maximum time to sing num songs. for Example len = ten, t[1] = 5, t[2] = 8, then f[5] to f[10] are equal to 1, can not know to sing from 5 to 10 which is the longest time to sing 1 songs. How to deal with it?

Here's a trick: a certain limit to your decisions! when calculating the songs that are sung at a certain time, it is necessary to finish the songs in that time, and the time is not enough. That is, F[j] represents the maximum number of songs that can be sung when the time of J is exactly exhausted. For example, the above examples are only f[5] and f[8] equals 1, the others are equal to 0. In this case, the maximum number of songs to sing is counted num, and then from j = Len begins to traverse the array F, and the first one equals num is the maximum time in the most songs.

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 10000;int F    [MAXN], T[55];int main () {int T, n, CAs, I, J, Len, MA;    scanf ("%d", &t);        for (cas = 1; CAs <= T; cas++) {scanf ("%d%d", &n, &len);                         len--;        Leave the last second to start singing Jin song for (i = 1; I <= n; i++) scanf ("%d", &t[i]);        memset (f, 0, sizeof (f));         Ma = 0;        Record the maximum number of songs. for (i = 1; I <= N, i++) {for (j = Len; J >= T[i]; j--) {if (F[j-t[i]) >= 1| |                j = = T[i])//In J time you can sing the first song.                    {F[j] = max (F[j], F[j-t[i]] + 1);                MA = Max (MA, f[j]);  }}} for (i = len; f[i]! = ma; i--);  Look for the most songs in the case, the longest time if (ma = = 0)//No song can be sung in a given time printf ("Case%d:%d%d\n",        CAS, 1, 678); else printf ("CasE%d:%d%d\n ", CAS, 1 + ma, i + 678); } return 0;}

UVA12563 Jin Ge jin Qu hao (DP, backpack + tips)

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.