12563-jin Ge Jin Qu Hao (DP)

Source: Internet
Author: User

I spent two hours a out of the feeling is not the same AH, study DP suggested that everyone more suffering a little, think about how the state is transferred.

But this problem I first understand wrong test instructions, otherwise will not waste so long time. The first thought is the knapsack problem, later found that the requirements to sing as much as possible, in this premise as far as possible to leave the KTV, I happen to be reversed.

So it's not hard to get a recursive equation: Because each track can only be sung once, so this makes the recursion orderly ~ then we set CNT[I][J] to indicate a number of songs before singing, and the total time does not exceed the maximum number of J songs.

Then cnt[i][j] = Max (Cnt[i-1][j],cnt[i-1][j-a[i]] + 1); Because you want to make the total time as long as possible, so when cnt[i][j] = = Cnt[i-1][j-a[i]] + 1 o'clock, d[i][j] = max (D[i][j],d[i-1][j-a[i]] + a[i]);

See the code for details:

#include <bits/stdc++.h>using namespace Std;const int maxn = 55;int T,n,t,case = 0,d[maxn][180*maxn + 678],a[maxn],c    NT[MAXN][180*MAXN + 678];int Main () {scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&t);        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);                for (int i=1;i<=n;i++) {for (int j=0;j<=t;j++) {Cnt[i][j] = (i = = 1? 0:cnt[i-1][j]);                D[I][J] = (i = = 1? 0:d[i-1][j]); if (J > a[i]) {if (Cnt[i][j] < Cnt[i-1][j-a[i]] + 1) {Cnt[i][j] = cnt[i-1]                        [J-a[i]] + 1;                    D[I][J] = D[i-1][j-a[i]] + a[i]; } else if (cnt[i][j] = = Cnt[i-1][j-a[i]] + 1) {D[i][j] = max (d[i][j],d[i-1][j-a[                    I]] + a[i]);    }}}} printf ("Case%d:%d%d\n", ++case,cnt[n][t]+1,d[n][t]+678); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

12563-jin Ge Jin Qu Hao (DP)

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.