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. python 在init方法中导入数据

  2. oracle如何看审计的结果

    1)数据库初始化参数文件中AUDIT_TRAIL=OS时,审计记录存在操作系统的文件中. UNIX系统的话,默认存在“$oracle_home/rdbms/audit/” 目录下. If you ha ...

  3. 使用 docker-compose 安装 MySQL 5.5 记录

    使用 docker-compose 安装 MySQL 5.5 记录 安装 Docker-Compose 在 Centos 中安装 Docker 倒是很简单. 但是安装 docker-compose 遇 ...

  4. oralce使用INSERT语句向表中插入数据

    INSERT   INTO    table[ (column [, column. . .])] VALUES            (value [,value . . .]); v  插入的数据 ...

  5. 序列化方案选型对比 - JSON/ProtocolBuffer/FlatBuffer/DIMBIN

    4千字长文预警!! 背景 JSON/XML不好吗? 好,再没有一种序列化方案能像JSON和XML一样流行,自由.方便,拥有强大的表达力和跨平台能力.是通用数据传输格式的默认首选.不过随着数据量的增加和 ...

  6. jquery解析XML文件实现的省市联动

    XML我是直接在网上下载的文件包 拿过来用的 jquery我用的是3.1的 前台页面 <form action="buy.html" method="get&quo ...

  7. Uva 10074【递推dp】

    UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...

  8. 基于PyTorch的Seq2Seq翻译模型详细注释介绍(一)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qysh123/article/detai ...

  9. 4.2.1 Data Flow-File Write-基本过程

  10. div 禁止点击

    今天需要这个需求,原来真的有 style="pointer-events: none;"