UVA 12563 Jin Ge Jin Qu hao(劲歌金曲)(01背包+滚动数组)

题意:在KTV唱歌剩下的t秒时间内,决定选最爱的n首歌中的一部分歌,在时间结束之前唱一首时长678秒的《劲歌金曲》,使得唱的总曲目尽量多(包括《劲歌金曲》),在此前提下尽量晚的离开KTV。(n<=50,t<=109)

分析:

1、输入保证所有n+1首曲子总长度严格大于t,虽然,t<=109,实际上t不会超过180n+678。

2、dp[i]剩余时间为i时唱的总曲目,time[i]剩余时间为i时唱歌时间总长度。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXT];
int time[MAXT];
int main(){
    int T;
    scanf("%d", &T);
    int kase = 0;
    while(T--){
        int n, t;
        scanf("%d%d", &n, &t);
        memset(dp, 0, sizeof dp);
        memset(time, 0, sizeof time);
        int len = Min(t - 1, 180 * n);
        for(int i = 0; i < n; ++i){
            int x;
            scanf("%d", &x);
            for(int j = len; j >= 0; --j){
                if(j >= x){
                    if(dp[j - x] + 1 > dp[j]){
                        dp[j] = dp[j - x] + 1;
                        time[j] = time[j - x] + x;
                    }
                    else if(dp[j - x] + 1 == dp[j]){
                        time[j] = Max(time[j], time[j - x] + x);
                    }
                }
            }
        }
        int ansnum = -1;
        int anstime = 0;
        for(int i = len; i >= 0; --i){
            if(dp[i] > ansnum){
                ansnum = dp[i];
                anstime = time[i];
            }
            else if(dp[i] == ansnum){
                anstime = Max(anstime, time[i]);
            }
        }
        printf("Case %d: %d %d\n", ++kase, ansnum + 1, anstime + 678);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6412720.html