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. window10下载安装jieba

    下载安装jieba的过程如下: 1 下载jieba 2 将其解压到E:\python2,如图所示: 3 在桌面左下角搜索框中输入"运行",之后输入"cmd".再 ...

  2. Cannot read property 'appendChild' of null

    1.js报错解决办法 这个一般是你获取的节点不存在引起的. 可能出现这种情况的原因:你获取这节点时,节点还没加载,例如:你的JS写在head里面,取body里面的某一节点,这时候是取不到的.这种情况的 ...

  3. shops

    #!/usr/bin/env python #coding:utf- import urllib2,sys,re,os,string reload(sys); sys.setdefaultencodi ...

  4. html Servlet web.xml(转)

    在浏览器输入:http://127.0.0.1:8080/test/test.html点击提交按钮,Tomcat后台输出:control: aaa's value is : bbb页面显示结果:pag ...

  5. 公司mysql问题二

    这个问题解决:

  6. fedora 安装ftp

    fedora默认不安装ftp服务(包括client程序/service程序),需要进行手动安装: yum install ftp(安装client) yum install vsftpd(安装serv ...

  7. concurrent模块

    concurrent包 concurrent.futrues模块 3.2版本引入 异步并行任务模块,提供一个高级的异步可执行的便利接口. 提供了两个池执行器 ThreadPoolExecutor异步调 ...

  8. Directx教程(22) 简单的光照模型(1)

    原文:Directx教程(22) 简单的光照模型(1)      在前面的教程中,我们在顶点属性中直接给顶点赋颜色,这样生成的三维物体缺乏真实感,如下图中两个立方体,左边的是通过光照生成物体表面颜色的 ...

  9. oracle-Expdp/impdp命令

    建立逻辑路径 create or replace directory dumpdir as 'c:\'; grant read,write on directory dumpdir to scott; ...

  10. python中defaultdict类

    回宿舍前翻翻Codeforces的时候发现了一个有趣的代码..其实是我没这么用过 :D 这是一份417B的代码 import sys from collections import defaultdi ...