博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hlg1201Zombie’s Treasure Chest---数学问题
阅读量:4449 次
发布时间:2019-06-07

本文共 3418 字,大约阅读时间需要 11 分钟。

 

Zombie’s Treasure Chest
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 86(17 users) Total Accepted: 16(13 users) Rating:  Special Judge: No
Description
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
Source
2011 Asia Shanghai Regional Contest
Recommend
齐达拉图

 

题目大意:

只有两种物品的背包,每件物品有一定的价值和体积,可以无限取,问一个n的袋子最多能装多大的价值。

数量级为int

分析:

看到这个题我的第一反应是完全背包,然后试着做了一下,发现数组存不下,然后又找地推关系,枚举一遍又超时了===好悲催

这个题的第一反应应该是有一部分一定是装性价比高的剩下的装性价比低的

顺着这个思路,我们这么考虑,对于s1 s2的最小公倍数 一定是s1,s2的整数倍   那么全装性价比高的物品一定为最优解  

也就是说对于最小公倍数的整数倍我们可以只装性价比高的物品 而剩余的容量单独处理

但是这儿有一个问题便是剩余是多少;

可以这样考虑:剩余的体积一定是由小于1物品小于s1和2物品小于s2的部分组成的

剩余的体积必须完全包含这两部分才可以

即n%lcm+lcm  枚举即可

 

代码:

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 long long gcd(long long a,long long b) 7 { 8 while(b) 9 {10 long long c=a%b;11 a=b;12 b=c;13 }14 return a;15 }16 17 long long get_gbs(long long a,long long b)18 {19 long long c=gcd(a,b);20 return a/c*b;21 }22 23 int main()24 {25 printf("%d\n",5%3);26 int t;27 long long n,s1,s2,v1,v2,num,gbs,ans1,ans2,left,pre;28 scanf("%d",&t);29 for(int kase=1; kase<=t; kase++)30 {31 scanf("%lld %lld %lld %lld %lld",&n,&s1,&v1,&s2,&v2);32 //if(s1
1)40 {41 left=n%gbs+gbs;42 pre=n-left;43 }44 else45 {46 pre=0;47 left=n;48 }49 ans1=ans2=0;50 if(v1*s2>v2*s1)51 ans1=pre/s1*v1;52 else53 ans1=pre/s2*v2;54 if(s1>=s2)55 {56 int xx=left/s1;57 for(int i=0; i<=xx; i++)58 {59 long long j=(left-s1*i)/s2;60 long long ss=v1*i+v2*j;61 if(ans2
View Code

 

 

转载于:https://www.cnblogs.com/zhanzhao/p/3659290.html

你可能感兴趣的文章
kylin cube 构建过程
查看>>
结合实际业务场景聊一聊MVP模式的应用
查看>>
scrapy实例:爬取中国天气网
查看>>
经济学效应
查看>>
深圳常见问题
查看>>
入侵指定网站的一些方法(思路篇)
查看>>
【帅刺猬】用鼠标Wheel中键控制对象的缩放
查看>>
如何设计Windows商店游戏的优秀磁贴
查看>>
支付宝一(配置私钥与公钥)
查看>>
[NOIP2017]时间复杂度(模拟)
查看>>
懒加载(延迟加载)
查看>>
前端学习笔记day03 清除浮动的四种方式
查看>>
JavaScript强化教程 - 六步实现贪食蛇
查看>>
【Nginx】请求上下文
查看>>
C++ 存储类
查看>>
整数游戏 (Integer Game,UVa 11489)
查看>>
项目Beta冲刺(团队) --3/7
查看>>
手机购买怎样识别假货——一点心得体会分享!
查看>>
scrapy-redis 分布式爬虫
查看>>
【JSP运行机制】
查看>>