zoukankan      html  css  js  c++  java
  • UVA

    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

    #include <iostream>
    #include <string.h>
    using namespace std;
    const int MAX = 50 * 60 * 3 + 1;
    struct Node {
        int n, t;
    
        bool operator<(const Node &rhs) const {
            return n < rhs.n ||
                    n == rhs.n && t < rhs.t;
        }
    }d[MAX];
    
    int main() {
        int n,cas,t,ti;
        cin >> cas;
        for (int cass = 1; cass <= cas; ++cass) {
            cin >> n >> t;
            memset(d, 0, sizeof(d));
            for (int i = 1; i <= n; ++i) {
                cin >> ti;
                for (int v = t; v > ti; --v) { //0,1背包,滚动数组
                    Node tmp;
                    tmp.n = d[v - ti].n + 1;
                    tmp.t = d[v - ti].t + ti;
                    if (d[v] < tmp)
                        d[v] = tmp;
                }
            }
            printf("Case %d: %d %d
    ", cass, d[t].n + 1, d[t].t + 678); //最后金曲也要算进去
        }
    }
  • 相关阅读:
    数据结构 括号法二叉树转化为二叉链表链式存储结构
    数据结构 二叉树的非递归遍历算法再回顾
    C语言算法 设计一个算法,将数组m个元素循环右移。要求算法空间复杂度为O(1)
    JAVA 递归输出所有可能的出栈序列
    C语言数据结构 头尾指针数组的综合应用
    C语言 重写strcmp函数
    C语言数据结构 判断出栈序列合法性
    PMD执行Java代码分析的原理
    Redis缓存和MySQL数据一致性方案详解
    mybtais 源码分析
  • 原文地址:https://www.cnblogs.com/wangsong/p/8017973.html
Copyright © 2011-2022 走看看