Jin Ge Jin Qu hao UVA - 12563 (劲歌金曲)01背包,求装入的东西最多(相同多时价值大)

题目:白书p274

题意: 
KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可. 
注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的. 
解析: 
每种歌曲只能用一次,所以是比较水的01背包题 
最后枚举找到最大曲目数量并且时间尽量靠后即可

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

(If you smiled when you see the title, this problem is for you ^_^)

For those who don’t know KTV, see: http://en.wikipedia.org/wiki/Karaoke_box

There is one very popular song called Jin Ge Jin Qu(). It is a mix of 37 songs, and is extremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and some other unofficial versions. But in this problem please forget about them.

Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you should select another song as soon as possible, because the KTV will not crudely stop a song before it ends (people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extra seconds! ....and if you select Jin Ge Jin Qu, you’ll get 663 extra seconds!!!

Now that you still have some time, but you’d like to make a plan now. You should stick to the following rules:

• Don’t sing a song more than once (including Jin Ge Jin Qu).

• For each song of length t, either sing it for exactly t seconds, or don’t sing it at all.

• When a song is finished, always immediately start a new song.

Your goal is simple: sing as many songs as possible, and leave KTV as late as possible (since we have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.

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<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
int t,m,n;
int dp[110];
int w[10000010];
int main()
{int k=1;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=0; i<m; i++)scanf("%d",&dp[i]);memset(w,-1,sizeof(w));w[0]=0;/**care*/for(int i=0; i<m; i++)for(int j=n-1; j>=dp[i]; j--)w[j]=max(w[j],w[j-dp[i]]+1);int ans=0,sum;for(int i=0;i<=n;i++){if(ans<=w[i]){ans=w[i];sum=i;}}printf("Case %d: %d %d\n",k++,ans+1,sum+678);}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310020.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记

任务16&#xff1a;oauth2 oidc 实现 client部分实现 client 之前启动一下上一节的 server&#xff0c;启动之前需要清除一些代码注释 Program 的 MigrateDbContextpublic static void Main(string[] args) {BuildWebHost(args)//.MigrateDbContext<ApplicationDbContext&g…

java中vi是什么意思_java中的public void是什么意思?

java中的public void是什么意思&#xff1f;发布时间&#xff1a;2020-05-23 11:21:22来源&#xff1a;亿速云阅读&#xff1a;255作者&#xff1a;Leahjava中的public void是什么意思&#xff1f;相信很多新手都不太了解&#xff0c;今天小编为了让大家更加了解public void的意…

[C++STL]常用算术生成算法

代码如下: #include <iostream> #include <vector> #include <numeric> using namespace std;void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}int total accumulate(v.begin(), v.end(), 0);cout << "total …

前端异步对象的原理与使用方法

源宝导读&#xff1a;现今互联网的WEB网站&#xff0c;几乎没有不用到JS异步技术的&#xff0c;虽然大家经常用到&#xff0c;但Javascript提供的异步机制如何才能真正用好呢&#xff0c;可能很多开发小伙伴还有点含糊&#xff0c;本文将从常见的开发场景出发&#xff0c;系统的…

Millenium Leapcow POJ - 2111 (千禧年跳牛)(贪心找最长路径,记忆化)

题意&#xff1a; 给你一个矩阵&#xff0c;问你按照象棋马的走法&#xff0c;下一步比上一步的数大&#xff0c;问长度最长的序列是多长&#xff0c;然后输出序列。如果有多个最长序列输出字典序最小的那个。类似滑雪&#xff0c;找出最长路径&#xff0c;多个答案 输出字典序…

java1.8的stream_JDK1.8新特性(一):stream

搜索热词一.什么是stream&#xff1f;1.概述Java 8 API添加了一个新的抽象称为流Stream&#xff0c;可以让你以一种声明的方式处理数据。这种风格将要处理的元素集合看作一种流&#xff0c; 流在管道中传输&#xff0c; 并且可以在管道的节点上进行处理&#xff0c; 比如筛选&a…

[C++STL]常用集合算法

代码如下: #include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std;class myPrint { public:void operator()(int val){cout << val << " ";} };void test01() {vector<int&g…

php给html传值,PHP传值到不同页面的三种常见方式及php和html之间传值问题_PHP

在项目开发中经常见到不同页面之间传值在web工作中&#xff0c;本篇文章给大家列出了三种常见的方式。接触PHP也有几个月了&#xff0c;本文总结一下这段日子中&#xff0c;在编程过程里常用的3种不同页面传值方法&#xff0c;希望可以给大家参考。有什么意见也希望大家一起讨论…

Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

题意&#xff1a;给一个字符串S&#xff0c; 求出所有前缀pre&#xff0c;使得这个前缀也正好是S的后缀。 输出所有前缀的结束位置。 就是求前缀和后缀相同的那个子串的长度 然后从小到大输出,主要利用next数组求解。 例如 “ababcababababcabab”&#xff0c; 以下这些前缀…

2020 年了,WPF 还有前途吗?

2020年了&#xff0c;微软的技术也不断更新了, .Net Core 3.1、.Net Framework 4.8以及今年11月推出的.NET 5...win10平台也普及了很多&#xff0c;WPF可以在上面大展身手&#xff0c;可性能和内存占用还是不行&#xff0c;但是WPF强大的UI能力很吸引人。WPF已经凉了吗? 学WPF…

[C++STL]常用查找算法

代码如下: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}vector<int>::iterator it find(v.begin(…

php asp 语法,ASP 语法

在我们的 ASP 教程中&#xff0c;每个实例都提供隐藏的 ASP 源代码。这样会使您更容易理解它们的工作原理。向浏览器写输出ASP 文件通常包含 HTML 标签&#xff0c;就像 HTML 文件。然而&#xff0c;ASP 文件也能包含服务器脚本&#xff0c;这些脚本被分隔符 包围起来。服务器脚…

#10003. 「一本通 1.1 例 4」加工生产调度(贪心)

加工生产调度 题目描述 某工厂收到了n个产品的订单&#xff0c;这n个产品分别在A、B两个车间加工&#xff0c;并且必须先在A车间加工后才可以到B车间加工。 某个产品i在A、B两车间加工的时间分别为Ai、Bi。询问怎样安排这n个产品的加工顺序&#xff0c;才能使总的加工时间最短…

不要把异常当做业务逻辑,这性能可能你无法承受

一&#xff1a;背景1. 讲故事在项目中摸爬滚打几年&#xff0c;应该或多或少的见过有人把异常当做业务逻辑处理的情况(┬&#xff3f;┬)&#xff0c;比如说判断一个数字是否为整数,就想当然的用try catch包起来&#xff0c;再进行 int.Parse&#xff0c;如果抛异常就说明不是整…

[设计模式]开闭原则

开闭原则: 对扩展开放&#xff0c;对修改关闭。 增加功能是提过增加代码来实现的&#xff0c;而不是去修改源代码。 代码如下: #include <iostream> #include <string> using namespace std;class Caculaor { public:Caculaor(int a,int b,string c):a(a),b(b…

#10010 「一本通 1.1 练习 6」糖果传递 (数学+贪心)

题目描述 原题来自&#xff1a;HAOI 2008 有 n个小朋友坐成一圈&#xff0c;每人有 ai 颗糖果。每人只能给左右两人传递糖果。每人每次传递一颗糖果的代价为 1 。求使所有人获得均等糖果的最小代价。 输入格式 第一行有一个整数 &#xff0c;n表示小朋友个数&#xff1b; …

ASP.NET Core在Docker下面生成简易验证码

背景 验证码这个功能是十分常见的&#xff0c;各大系统的登录页面都会有。今天介绍一下最为普通的验证码。无论最终给到前端的是图片格式的验证码还是base64格式的验证码&#xff0c;其实都离不开这样的一步操作&#xff0c;都要先在后台生成一个图片。就个人经验来说&#xff…

[设计模式]迪米特法则

迪米特法则 又叫最少知识法则 类中的成员属性和成员方法&#xff0c;如果不需要对外暴露&#xff0c;就不要设成public。 代码如下: #include <iostream> #include <string> using namespace std;class AbstractBuilding { public:virtual void sale() 0; };cl…

#10017 「一本通 1.2 练习 4」传送带+三分套三分

题目描述 原题来自&#xff1a;SCOI 2010 在一个 2 维平面上有两条传送带&#xff0c;每一条传送带可以看成是一条线段。两条传送带分别为线段 AB和线段CD 。lxhgww 在 AB上的移动速度为 P &#xff0c;在 CD上的移动速度为 Q &#xff0c;在平面上的移动速度R 。 现在 l…

java世博会,反应原生失去的世博会

我用react-init创建的我的RN项目工作正常&#xff0c;直到我做 npm install 才能让 react-native-elements 与 react-native-vector-icons 一起工作 . 我无法链接&#xff0c;它说我有反应本地而不是react-native-cli . 所以我卸载并反应原生并全局安装cli .现在 npm start 告诉…