Uva 12563 Jin Ge Jin Qu hao(01背包)
题意:
假定你在唱KTV,还剩下t秒时间。你决定接下来唱你最喜爱的n首歌(不包含劲歌金曲)中的一些歌曲。在时间结束之前再唱一个劲歌金曲。使得唱的歌的总曲目尽量多以及时间总长度。
输入保证所有n+1曲子的时间长度严格大于t。
题解:
每一首歌的时间长度不超过3分钟,最多50首歌,所以时间t最大也是180*n+678。这样就可以转化为01背包问题。
但是这个问题有约束条件:唱的歌曲尽量多,和时间总长要长。因为第一个的条件优先。所以:
当num[j - w[i]] + 1 > num[j] 时,更新歌曲数和用时。
当num[j - w[i]] + 1 == num[j] 时,只更新时间。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x7fffffff;
const int maxn = +;
const int mod = ;
int w[];
int dp[];
int num[];
int kase = ;
void init()
{
ms(w, );
ms(dp, );
ms(num, );
}
void solve()
{
int n, t, sum = ;
scanf("%d%d", &n, &t);
for(int i = ;i<=n;i++) scanf("%d", &w[i]), sum+=w[i];
for(int i = ;i<=n;i++){
for(int j = t- ; j >= w[i];j--){
if(num[j-w[i]] + > num[j] ){
num[j] = num[j-w[i]] + ;
dp[j] = dp[j-w[i]] + w[i];
}
else if(num[j-w[i]] + == num[j] && dp[j-w[i]] + w[i] > dp[j] ){
dp[j] = dp[j-w[i]] + w[i];
}
}
}
printf("Case %d: %d %d\n", kase++, num[t-]+, dp[t-]+*+);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int T;
scanf("%d", &T);
while(T--){
init();
solve();
}
return ;
}
你努力的时候,比你厉害的人也在努力。
Uva 12563 Jin Ge Jin Qu hao(01背包)的更多相关文章
- UVA - 12563 Jin Ge Jin Qu hao (01背包)
InputThe first line contains the number of test cases T (T ≤ 100). Each test case begins with two po ...
- UVA12563Jin Ge Jin Qu hao(01背包)
紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲 ...
- UVA Jin Ge Jin Qu hao 12563
Jin Ge Jin Qu hao (If you smiled when you see the title, this problem is for you ^_^) For those who ...
- uVa 12563 Jin Ge Jin Qu
分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...
- 12563 - Jin Ge Jin Qu hao——[DP递推]
(If you smiled when you see the title, this problem is for you ^_^) For those who don’t know KTV, se ...
- 12563 Jin Ge Jin Qu hao
• Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...
- UVa 12563 (01背包) Jin Ge Jin Qu hao
如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...
- Jin Ge Jin Qu hao UVA - 12563 01背包
题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...
- 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 【紫书】(UVa12563)Jin Ge Jin Qu hao
继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...
随机推荐
- 【openstf】自己的云测平台——mac安装openstf
openstf的github地址:https://github.com/openstf/stf 上图可以清晰看出openstf的使用场景和效果 openstf是一个web应用程序,用于远程调试智能 ...
- python批量下载验证码,用来做验证码处理
刚学到爬虫识别验证码,所以自己建一个获取验证码的类,感兴趣的道友,可以看看,代码如下: import requests import time import os import re class Pi ...
- 如何搭建Vue环境?
搭建vue的开发环境: https://cn.vuejs.org/v2/guide/installation.html 1. 必须要安装nodejs cnpm 下载包的速度更快一些. 地址: ...
- redis两种持久化的方法
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...
- [Git] 005 初识 Git 与 GitHub 之分支
在 GitHub 的 UI 界面使用 Git(多图警告) 1. 建立分支 1.1 点击左上方的 Branch: master,在输入框中填入分支名,再点击下方的 Create branch 1.2 此 ...
- RocketMQ安装部署及整合Springboot
消息中间件的功能: 通过学习ActiveMq,kafka,rabbitMq这些消息中间件,我们大致能为消息中间件的功能做一下以下定义:可以先从基本的需求开始思考 最基本的是要能支持消息的发送和接收,需 ...
- Git-第五篇廖雪峰Git教程学习笔记(4)分支
1.一开始,只有一个主分支(master),HEAD指向Master,而Master指向主分支.现在我们创建dev分支. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/git ...
- centos解决Could not find a version that satisfies the requirement pip3 (from versions: none)及No matching distribution found for pip3问题
python环境:python 3.8 报错信息: WARNING: pip is configured with locations that require TLS/SSL, however th ...
- node+mysql+express实现登录/注册/修改密码/删除用户 接口
实现用户的注册.登录.修改密码.删除用户操作 用到的数据库:nodecms:表:user 目录结构: db目录下存放数据库操作语句: userSQL.js 用户有关的操作语句 router目录 接口路 ...
- Java中的集合详解及代码测试
1:对象数组 (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. 2:集合(Collection) (1)集合的由来 我们学习的是Java -- 面向对象 ...