UVA12563 Jin Ge Jin Qu hao

Descriprion

If you smiled when you see the title, this problem is 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 Jin Qu(). It is a mix of 37 songs, and is extremelylong (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and some otherunofficial versions. But in this problem please forget about them.Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you shouldselect another song as soon as possible, because the KTV will not crudely stop a song before it ends(people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extraseconds! ....and 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 should stick to thefollowing rules:

• Don’t sing a song more than once (including Jin Ge Jin Qu)

• For each song of length t, either sing it for exactly t 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 (since wehave rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.

Input Instruction

The first line contains the number of test cases T (T ≤ 100). Each test case begins with two 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 will be less than 3 minutes — I know that most songs are longer than 3 minutes.But don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends.So here “length” actually means “length of the part that we want to sing”.It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly largerthan t.

Output Instruction

For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengthsof songs that you’ll sing.Explanation:In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qufor another 678 seconds.In the second example, we sing the first two (30+69=99 seconds). Then we still have 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 only have 100 seconds in total), so wecan’t sing Jin Ge Jin Qu anymore!

Sample Input

2
3 100
60 70 80
3 100
30 69 70

Sample Output

Case 1: 2 758
Case 2: 3 777

Analysis

找小于T的满足双条件最值,可以每步判断-->取端点,也可以最后判断-->取最值

Code

法1:每次双条件比较

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,t,i,j,r;
    scanf("%d",&n);
    for(i=0;i<n;++i){
        scanf("%d%d",&m,&t);
        vector<int>dp(t+1,0),has(t+1,0);
        for(j=1;j<=m;++j){
            scanf("%d",&r);
            for(int k=t;k>r;--k){
                if(dp[k-r]+1>dp[k]){dp[k]=dp[k-r]+1;has[k]=has[k-r]+r;}
                else if(dp[k-r]+1==dp[k]&&has[k-r]+r>has[k])has[k]=has[k-r]+r;
            }
        }
        printf("Case %d: %d %d\n",i+1,dp[t]+1,has[t]+678);
    }
    return 0;
}

法2:去掉不可达结点+最后比较

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,t,i,j,r,ans;
    scanf("%d",&n);
    for(i=0;i<n;++i){
        scanf("%d%d",&m,&t);
        vector<int>dp(t+1,-1);dp[0]=0;
        for(j=1;j<=m;++j){
            scanf("%d",&r);
            for(int k=t-1;k>=r;--k)dp[k]=max(dp[k],dp[k-r]+1);
        }
        for(ans=j=t-1;j>=0;--j)if(dp[ans]<dp[j])ans=j;
        printf("Case %d: %d %d\n",i+1,dp[ans]+1,ans+678);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chanceYu/p/12083401.html