UVa OJ 12563 - Jin Ge Jin Qu hao

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/The_MADAO/article/details/52653911

UVa OJ 12563 - Jin Ge Jin Qu hao

Problem

Here is the: link

Solution

这道题目一开始用时间作为一个大循环去进行递推,但是发现并不是很好判断歌曲是否有唱过,所以后来还是采用歌曲作为大循环,再以时间为小循环。不过记得要留出1s来给《劲歌金曲》

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int T, n, t, cas, ans;
int song[51], dp[9001];
long long lis[9001];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt" , "r", stdin );
    //freopen("output.txt", "w", stdout);
    cin >> T;
    while(T--) {
        cin >> n >> t;
        memset(dp, 0x8f, sizeof(dp));
        dp[0] = 0;
        ans = t-1;
        for (int i = 0; i < n; ++i) cin >> song[i];
        for (int i = 0; i < n; ++i) 
            for (int j = t-1; j >= song[i]; --j) 
                dp[j] = max(dp[j], dp[j-song[i]]+1);
        for (int i = t-1; i >=0; --i)
            ans = dp[i]>dp[ans] ? i : ans;
        cout << "Case " << ++cas << ": "
             << dp[ans]+1 << ' ' << ans+678 << '\n';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The_MADAO/article/details/52653911