UVa 12325 - Zombie's Treasure Chest-[分类枚举]
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-[分类枚举]的更多相关文章
- Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)
题意: 你有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号的整数.你的任务是最多能装多少价值的宝物? 分析: 分类枚举, ...
- uva 12325 Zombie's Treasure Chest
https://vjudge.net/problem/UVA-12325 题意: 一个箱子,体积为N 两种宝物,体积为S1.S2,价值为V1.V2,数量无限 最多装多少价值的宝物 数据范围:2^32 ...
- UVA - 12325 Zombie's Treasure Chest (分类搜索)
题目: 有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号整数.计算最多能装多大价值的宝物,每种宝物都必须拿非负整数个. 思 ...
- UVa 12325 Zombie's Treasure Chest【暴力】
题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 #include<iostream> #inclu ...
- UVA 12325 Zombie'sTreasureChest 宝箱 (分类枚举)
看上去非常像背包的问题,但是体积太大了. 线性规划的知识,枚举附近点就行了,优先选性价比高的, 宝物有两种体积为S0,价值V0,体积S1,价值V1. 枚举分以下几种: 1:枚举拿宝物1的数量,然后尽量 ...
- hdu 4091 Zombie’s Treasure Chest 贪心+枚举
转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...
- G - Zombie’s Treasure Chest(动态规划专项)
G - Zombie’s Treasure Chest Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 一道看似dp实则暴力的题 Zombie's Treasure Chest
Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是 ...
- HDU 4091 Zombie’s Treasure Chest 分析 难度:1
Zombie’s Treasure Chest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
随机推荐
- JQuery-- 实例:小米左右切图,淡入淡出,自动,小圆点触发轮播图
示意图: demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- CSS中的margin和padding的用法和区别
在CSS中margin是指从自身边框到另一个容器边框之间的距离,就是容器外距离. 语法结构 (1)padding-left:10px; 左内边距 (2)padding-right:10px; 右内边距 ...
- oracle加锁
锁 insert into TEST values(5); 加row exclusive锁,和row share相同,但也禁止用share方式加锁. Create index idx_test on ...
- Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母
给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...
- Length of Last Word输出最后单词的字母个数
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [React Native]升级React Native版本
React Native正式版本还没发布,但是小版本基本上每个月都更新1-2次.9月11号又更新了0.33版本,其中有两个增强功能正好是项目中用到的. 添加Android6.0权限验证API Add ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- php后端语言的基本语法
<?php$num = 1;//php中定义一个变量echo $num;//php中打印一个值(与console.log类似)$arr = array(1,2,3,4,5,6,7,89);//在 ...
- 关于使用 Java 分片读\写文件
分片读取文件方法: /** * 分片读取文件块 * * @param path 文件路径 * @param position 角标 * @param blockSize 文件块大小 * @return ...
- DRDS 数据恢复重磅发布,全方位保障您的数据安全
背景介绍 数据库存储着企业的核心数据,在企业中占据非常重要的位置,一旦出现SQL注入,数据误删的情况,影响的不仅仅是业务,还会泄露用户的个人信息.因此,数据库的数据安全问题十分重要. 当数据库迁移到云 ...