UVA 12563 Jin Ge Jin Qu hao 01背包变形

基本的01背包,更新的时候保持背包里每一个元素的num最大然后time尽量长

CSDN也支持makedown了试一下



12563 Jin Ge Jin Qu hao

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

There is one very popular song called Jin Ge Jin Qu(劲歌金曲). It is a mix of 37 songs, and is

extremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and

some other unofficial 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 should

select 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 extra

seconds! ….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 the

following 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 we

have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.

Input

The first line contains the number of test cases T (T ≤ 100). Each test case begins with two 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 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 larger

than t.

Output

For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths

of 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 Qu

for another 678 seconds.

In the second example, we sing the first two (30+69=99 seconds). Then we still have one second

left, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third song

instead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so we

can’t sing Jin Ge Jin Qu anymore!Universidad de Valladolid OJ: 12563 – Jin Ge Jin Qu hao 2/2

Sample Input

2

3 100

60 70 80

3 100

30 69 70

Sample Output

Case 1: 2 758

Case 2: 3 777

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月08日 星期日 10时45分24秒
File Name     :UVA12563_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

int n,t,ti[111];

struct ST
{
    int num,time;
};

ST dp[110][11000];

bool check(int a,int b,int time)
{
    int c=a-1 , d=b-time;
    if((dp[c][d].num+1>dp[a][b].num)
        ||((dp[c][d].num+1==dp[a][b].num)
            &&(dp[c][d].time+time>=dp[a][b].time)))
                return true;
    else return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d",&n,&t);
        int sumtime=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",ti+i);
            sumtime+=ti[i];
        }
        if(sumtime<t)
        {
            printf("Case %d: %d %d\n",cas++,n+1,sumtime+678);
            continue;
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<t;j++)
            {
                dp[i][j]=dp[i-1][j];
                if(j-ti[i]>=0)
                {
                    if(check(i,j,ti[i]))
                    {
                        dp[i][j].num=dp[i-1][j-ti[i]].num+1;
                        dp[i][j].time=dp[i-1][j-ti[i]].time+ti[i];
                    }
                }
            }
        }

        ST ok=dp[n][0];
        for(int i=1;i<t;i++)
        {
            ST e = dp[n][i];
            if((e.num>ok.num)||(e.num==ok.num&&e.time>ok.time))
                ok=e;
        }

        printf("Case %d: %d %d\n",cas++,ok.num+1,ok.time+678);
    }

    return 0;
}
时间: 2024-10-21 18:51:31

UVA 12563 Jin Ge Jin Qu hao 01背包变形的相关文章

UVA 12563 Jin Ge Jin Qu hao(01背包变形:两个背包内容)

题意: KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可. 注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的. //0 KB 39 ms #include<cstdio> #include<cstring>

UVa 12563 Jin Ge Jin Qu hao(01背包)

题意  你在KTV还剩t秒钟的时间  你需要在n首歌中选择尽量多的歌使得歌的数量最多的前提下剩下的时间最小 至少要留一秒给劲歌金曲  所以是一个容量为t-1的01背包   d[i][j]表示恰用j秒时间在前i首歌中最多唱多少首  每个状态有两种选择 唱或不唱第i首歌 所以有转移方程d[i][j]=max(d[i-1][j],d[i-1][j-c[i]]+1) #include <bits/stdc++.h> using namespace std; const int N = 55, M =

UVA12563Jin Ge Jin Qu hao(01背包)

紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲,设两个Time求时间,cnt是数量 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int MAX

uva 12563 - Jin Ge Jin Qu hao(动态规划~劲!歌!金!曲!)

错的我真是无语...还是状态的把握不准确.. 起始状态转移方程是很重要,但是只推出了方程是不够的 对边界状态的处理,对特殊状态的处理,这些都很重要,错了任何一个小地方,都会导致WA.... 细节!更清晰的思路,更全面的考虑! #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,t; int v[55]; int d[55][10000]; int path[5

UVA Jin Ge Jin Qu hao 12563

Jin Ge Jin Qu hao (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_box There is one very popular song called Jin Ge Jin Qu(). It is a mix of 37 songs, and is e

12563 - Jin Ge Jin Qu hao——[DP递推]

(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_box There is one very popular song called Jin Ge Jin Qu(). It is a mix of 37 songs, and is extremely long (11

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

codeforce Gym 101102A Coins (01背包变形)

01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXW 15005 #define N 155 #define LL long long #define MOD 1000000007 int w1[N],w2[N]; LL dp1[MAXW],dp2[MAXW]; int main(

Wikioi 1025 01背包变形

这题多加了菜品必选编号,所以刚开始不知道怎么写,原来就把必选的处理下就行了,因为有重复,但是相同的价值与价格都一样,所以这里就直接挑出来就行了. 把不是必选的在里面用dp即可,dp之前也要把重复的舍去. 因为总价格容量为浮点数,所以先乘以10变成整数就可以用01背包了. #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque&