G - Zombie’s Treasure Chest

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
题意:一些战士来到了一个村庄,发现了两种宝石和一个宝箱,每种宝石的个数是无限的,给出了宝箱的大小 N 和两种宝石的 size(体积) 和 value(价值) ,即 N s1 v1 s2 v2 ,已知这五个两,求利用这个宝箱,战士能够带走多大价值的宝石。 五个数的范围是 int 。
思路:
算法思路:1.假设变量N,S1,V1,S2,V2
                         2.求S1,S2的最小公倍数LCM
                         3.求商s=N/LCM,余数y=N%LCM
                         4.s>=1 则s--,y+=LCM

                         5.然后就是枚举,从nsize枚举到0,注意这之前有个很重要的技巧避免超时:枚举s1,s2中拿的量少的
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue> using namespace std; long long gcd(long long a,long long b)
{
if(b==) return a;
return gcd(b,a%b);
} int main()
{
freopen("1.txt","r",stdin);
int cas,t;
long long n,s1,v1,s2,v2;
long long g,l,res,rv1,rv2,i,mx,tmp,j;
cin>>t;
for(cas=;cas<=t;cas++)
{
cin>>n>>s1>>v1>>s2>>v2;
if(s1>s2)
{
swap(s1,s2);
swap(v1,v2);
}
g=gcd(s1,s2);
l=s1/g*s2;
rv1=l/s1*v1;
rv2=l/s2*v2;
if(n>=l+l)
{
res=(n-l)/l*(rv1>rv2?rv1:rv2);
n=n%l+l;
}
else
{
res=;
}
mx=;
for(i=,j=;i<=n;i+=s2,j++)
{
tmp=j*v2+(n-i)/s1*v1;
if(tmp>mx)
{
mx=tmp;
}
}
res+=mx;
cout<<"Case #"<<cas<<": "<<res<<endl;
}
return ;
}

最新文章

  1. mybatis_常用标签
  2. 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)
  3. leetcode Pow(doubule x,int n)
  4. android基本知识(一)
  5. 关于textView的字数限制
  6. StringBuilder[] 作为数组如何使用
  7. C/C++语言的标准库函数malloc/free与运算符new/delete的区别
  8. 关于mysql查询数据库时间和系统时间差
  9. 04 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之高并发优化
  10. 《java入门第一季》之Character类小案例
  11. 【JVM】JVM随笔索引
  12. 4.28Linux(6)
  13. 解决UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-1: ordinal not in range
  14. 移动端rem适配 flex.js
  15. 关于namespace的使用
  16. SSL和TLS协议的区别
  17. 《Linux内核分析》 第五节 扒开系统调用的三层皮(下)
  18. Docker命令之 cp
  19. HDU 4764 Stone (巴什博弈)
  20. 利用signapk.jar工具对apk文件进行签名

热门文章

  1. Unity3D脚本使用:游戏对象访问
  2. PWM
  3. Linux 下搭建jsp服务器(配置jsp开发环境)
  4. 利用requestjs优化响应式移动端js加载
  5. JQuery笔记(三)选项卡
  6. Java基础之异常
  7. Java集合初体验
  8. C++友元
  9. removeObjectAtIndex
  10. QTP脚本汇总比较有价值