zoukankan      html  css  js  c++  java
  • UVA12563-Jin Ge Jin Qu hao(动态规划基础)

    Problem UVA12563-Jin Ge Jin Qu hao

    Accept: 642  Submit: 7638
    Time Limit: 3000 mSec

    Problem Description

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

     Sample Input

    2
    3 100
    60 70 80
    3 100
    30 69 70
     

    Sample Output

    Case 1: 2 758

    Case 2: 3 777

    题解:普通的01背包,这里状态的定义为到第t秒最多唱几首歌,而不是t秒内最多,因此初始化注意一下,除了dp[0]=0,其余都要定义成一个不可能被转移过去的值(大的负数就好),背包九讲里有讲解,对于前0首歌,只有0秒唱0首歌是一个合法的状态,其余都是不合法的,因此不能让他们转移过去。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int read() {
     6     int q = 0, f = 1;
     7     char ch = ' ';
     8     while (ch < '0' || '9' < ch) {
     9         if (ch == '-') f = -1;
    10         ch = getchar();
    11     }
    12     while ('0' <= ch && ch <= '9') {
    13         q = q * 10 + ch - '0';
    14         ch = getchar();
    15     }
    16     return q * f;
    17 }
    18 
    19 const int maxn = 50 + 5, maxt = 10000 + 10;
    20 const int INF = 0x3f3f3f3f;
    21 
    22 int n, t;
    23 int T = 1;
    24 int ti[maxn], dp[maxt];
    25 
    26 int main()
    27 {
    28     //freopen("input.txt", "r", stdin);
    29     int iCase;
    30     iCase = read();
    31     while (iCase--) {
    32         n = read(), t = read();
    33         t--;
    34         int sum = 0;
    35         for (int i = 0; i < n; i++) {
    36             ti[i] = read();
    37             sum += ti[i];
    38         }
    39 
    40         for (int i = 1; i <= t; i++) {
    41             dp[i] = -INF;
    42         }
    43         dp[0] = 0;
    44         for (int i = 0; i < n; i++) {
    45             for (int j = t; j >= ti[i]; j--) {
    46                 dp[j] = max(dp[j], dp[j - ti[i]] + 1);
    47             }
    48         }
    49 
    50         int ans = 0, last = 0;
    51         for (int i = 0; i <= t; i++) {
    52             if (ans <= dp[i]) {
    53                 ans = dp[i];
    54                 last = i;
    55             }
    56         }
    57         last += 678;
    58         printf("Case %d: %d %d
    ", T++, ans + 1, last);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    Git Extensions system.invalidoperationexception尚未提供文件名,因此无法启动进程
    批处理将文件夹下的文件名称写入到一个文本里
    hdwiki 参考资料改成可点击跳转的(默认不能点)
    执行存储过程报错——ora-01031:权限不足
    Web Service——CXF发布REST服务
    Web Service——CXF+Spring 整合
    Web Service——CXF
    xmlhttp.readyState==4 && xmlhttp.status==200的探究
    XMLHttpRequest cannot load ...谷歌浏览器跨域问题
    Web Service基础——四种客户端调用方式
  • 原文地址:https://www.cnblogs.com/npugen/p/9729634.html
Copyright © 2011-2022 走看看