12325 Zombie’s Treasure

  Chest Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies. The warriors are so brave that they decide to defeat the zombies and then bring all the treasures back. A brutal long-drawn-out battle lasts from morning to night and the warriors find the zombies are undead and invincible. Of course, the treasures should not be left here. Unfortunately, the warriors cannot carry all the treasures by the treasure chest due to the limitation of the capacity of the chest. Indeed, there are only two types of treasures: emerald and sapphire. All of the emeralds are equal in size and value, and with infinite quantities. So are sapphires. Being the priest of the warriors with the magic artifact: computer, and given the size of the chest, the value and size of each types of gem, you should compute the maximum value of treasures our warriors could bring back.

Input

  There are multiple test cases. The number of test cases T (T ≤ 200) is given in the first line of the input file. For each test case, there is only one line containing five integers N, S1, V1, S2, V2, denoting the size of the treasure chest is N and the size and value of an emerald is S1 and V1, size and value of a sapphire is S2, V2. All integers are positive and fit in 32-bit signed integers.

Output

  For each test case, output a single line containing the case number and the maximum total value of all items that the warriors can carry with the chest.

Sample Input

2

100 1 1 2 2

100 34 34 5 3

Sample Output

Case #1: 100

Case #2: 86

解题思路:

  一般的思想是枚举宝物1(假设宝物s1>s2,当s2>s1时相反)的数量n/s1,然后尽可能多拿宝物2,得到最大价值。这种方法的时间复杂度是

o(n/Smax)。但是当n/Smax很大时这种方法就行不通了。幸运的当n>>s1,s2时可以比较他们的性价比(价值v/大小s):

  当v1/s1<v2/s2时,说明宝物2的性价比高,那么宝物1最多只能拿s2-1个,因为,假如宝物1的数量>=s2,那么可以将s2个宝物1的换成s1个宝物2,体积不变,价值增加,这种情况下枚举量就是s2-1。时间复杂度为o(Smax-1);

如何定义远大于呢?可以认为n/Smax >10^5时就是远大于了,因为此时最大枚举量不过10^5,不影响效率。

代码如下:

 //
// main.cpp
// Zombie's Treasure Chest
//
// Created by 胡佳成 on 16/3/25.
// Copyright © 2016年 胡佳成. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <ctime>
#include <algorithm>
#define print_time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
using namespace std;
int N,S1,V1,S2,V2;
const int limit=;
typedef long long LL;
LL most_value(int num_s2,int s1,int v1,int s2,int v2){//默认枚举s2
LL val=;
for(LL i=;i<=num_s2;i++)
val=max(val,(N-i*s2)/s1*v1+i*v2); return val;
}
int main() {
int T;
scanf("%d",&T);
for(int i=;i<=T;i++){
scanf("%d%d%d%d%d",&N,&S1,&V1,&S2,&V2);
LL value=;
if(S1>S2){
swap(S1, S2);
swap(V1, V2);
}
if(N/S2<=limit){
value=most_value(N/S2, S1, V1, S2, V2);
}
else if(LL(S2)*V1<LL(S1)*V2){
value=most_value(S2-, S2, V2, S1, V1);
}
else{
value=most_value(S1-, S1, V1, S2, V2);
}
printf("Case #%d: %lld\n",i,value);
} //print_time_;
return ;
}

  

  

UVa 12325 - Zombie's Treasure Chest-[分类枚举]的更多相关文章

  1. Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)

    题意: 你有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号的整数.你的任务是最多能装多少价值的宝物? 分析: 分类枚举, ...

  2. uva 12325 Zombie's Treasure Chest

    https://vjudge.net/problem/UVA-12325 题意: 一个箱子,体积为N 两种宝物,体积为S1.S2,价值为V1.V2,数量无限 最多装多少价值的宝物 数据范围:2^32 ...

  3. UVA - 12325 Zombie's Treasure Chest (分类搜索)

    题目: 有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号整数.计算最多能装多大价值的宝物,每种宝物都必须拿非负整数个. 思 ...

  4. UVa 12325 Zombie's Treasure Chest【暴力】

    题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 #include<iostream> #inclu ...

  5. UVA 12325 Zombie'sTreasureChest 宝箱 (分类枚举)

    看上去非常像背包的问题,但是体积太大了. 线性规划的知识,枚举附近点就行了,优先选性价比高的, 宝物有两种体积为S0,价值V0,体积S1,价值V1. 枚举分以下几种: 1:枚举拿宝物1的数量,然后尽量 ...

  6. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  7. G - Zombie’s Treasure Chest(动态规划专项)

    G - Zombie’s Treasure Chest Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  8. 一道看似dp实则暴力的题 Zombie's Treasure Chest

     Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是 ...

  9. HDU 4091 Zombie’s Treasure Chest 分析 难度:1

    Zombie’s Treasure Chest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

随机推荐

  1. 在liferay 7中如何删除service builder已经生成的数据库table

    在Liferay 7中,加了数据库保护机制,你改了service.xml的结构后,重新运行service builder,并不会帮你生成新的数据库表.然后你发现你在数据库中自己手动删除了表后,重新部署 ...

  2. 【JZOJ3885】【长郡NOIP2014模拟10.22】搞笑的代码

    ok 在OI界存在着一位传奇选手--QQ,他总是以风格迥异的搞笑代码受世人围观 某次某道题目的输入是一个排列,他使用了以下伪代码来生成数据 while 序列长度<n do { 随机生成一个整数属 ...

  3. Facebook POP 进阶指南

    本文转自Kevin Blog Facebook 在发布了 Paper 之后,似乎还不满足于只是将其作为一个概念性产品,更进一步开源了其背后的动画引擎 POP,此举大有三年前发布的 iOS UI 框架  ...

  4. 2018-11-21-WPF-解决-ViewBox--不显示线的问题

    title author date CreateTime categories WPF 解决 ViewBox 不显示线的问题 lindexi 2018-11-21 09:37:53 +0800 201 ...

  5. 云数据库RDS存储能力进化解析!

    数据库是企业IT系统的核心,其性能表现会直接影响整体业务系统的性能表现,而影响数据库性能因素包括系统架构设计.应用程序业务SQL语句.数据库参数优化配置.数据库运行的资源能力.系统架构设计和应用程序业 ...

  6. 阿里云DDoS高防的演进:防御效果成核心

    分布式拒绝服务(DDoS)攻击这一网络公敌,是任何互联网业务的重大威胁.随着DDoS攻击工具化的发展,无论是简单野蛮的流量型攻击,还是复杂精巧的应用型攻击,黑客发起DDoS攻击变得越来越简单和自动化. ...

  7. BZOJ 1834网络扩容题解

    一道不算太难的题目 但是真的很恶心 显然,对于第一问,我们直接无脑打模板就好了 第二问也不是很难,我们将每条边再连一条容量为inf,费用为w的边 但是流量只要小于第一问的答案加k就行了 所以我们增加一 ...

  8. Vue.js 第1章 Vue常用指令学习

    今日目标 能够写出第一个vue应用程序 能够接受为什么需要学vue 能够使用指令 能够通过指定完成简单的业务(增加删除查询) 能够理解mvvm 为什么要学习vue 企业需要 可以提高开发效率 实现vu ...

  9. jQuery 加法计算 使用+号即强转类型

    var value1 = $("#txt1").val(); var value2 = $("#txt2").val(); //数值前添加+号 number加号 ...

  10. Python基础:21包装

    “包装”在Python 编程中经常会被提到的一个术语.意思是对一个已存在的对象进行包装,可以是对一个已存在的对象,增加,删除,或者修改功能. 可以包装任何类型(type)作为一个类(class)的核心 ...