[01背包] Jin Ge Jin Qu hao UVa12563

题意

假定你正在唱KTV,还剩下t秒时间。你决定只唱你最爱的n首歌,并在时间结束之前再唱一首《劲歌金曲》,使得唱的总曲目尽量多,在此前提下尽量晚的离开KTV。

题解

题目中t的范围是1e9但实际的t不会超过10000,这就可以转化为01背包问题。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

const int maxn = 10000+1;
const int INF = 0x3f3f3f3f;

struct pii{
    int cnt, time;
    pii(int c = 0, int t = 0):cnt(c),time(t){}
    bool operator < (const pii &rhs)const{
        return cnt<rhs.cnt || (cnt==rhs.cnt && time<rhs.time);
    }
    pii operator = (const pii &rhs){
        cnt = rhs.cnt;
        time = rhs.time;
        return *this;
    }
};

pii operator + (pii rhs, int t){
    rhs.cnt++;
    rhs.time += t;
    return rhs;
}

int n,m;
pii dp[51][maxn];
int t[maxn];

int main(){
    int T, kase = 0;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &m);
        for(int i = 0; i<n; i++)
            scanf("%d", t+i);
        for(int i = 1; i<=m; i++){
            dp[0][i] = pii(0,0);
            if(i > t[0]){
                dp[0][i] = pii(1,t[0]);
            }
        }
        for(int i = 1; i<n; i++)
        for(int j = 1; j<=m; j++){
            dp[i][j] = dp[i-1][j];
            if(j > t[i])
                dp[i][j] = max(dp[i][j], dp[i-1][j-t[i]] + t[i]);
        }
        printf("Case %d: ", ++kase);
        printf("%d %d\n",dp[n-1][m].cnt+1, dp[n-1][m].time+678);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值