当前位置:网站首页>【练习-9】Zombie’s Treasure Chest
【练习-9】Zombie’s Treasure Chest
2022-07-06 09:26:00 【火焰车】
题目大意:
一些勇敢的战士来到一个失落的村庄。他们非常幸运,发现了很多宝藏和一个大宝箱,但也发现了愤怒的僵尸。
战士们非常勇敢,他们决定打败僵尸,然后把所有的宝藏都带回来。一场旷日持久的残酷战斗从早到晚持续着,战士们发现僵尸是不死的,不可战胜的。
当然,宝藏不应该留在这里。不幸的是,由于宝箱容量的限制,战士们无法用宝箱携带所有的宝藏。事实上,珍宝只有两种:祖母绿和蓝宝石。所有的祖母绿在大小和价值上都是相等的,而且数量是无限的。蓝宝石也是。
身为拥有魔法神器的战士们的牧师:电脑,考虑到箱子的大小,每种宝石的价值和大小,你应该计算出我们战士们能带回的宝藏的最大价值。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
struct node{
ll s,v;
};
node mx,mn,a[2];
int main()
{
int t,V;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>V>>a[0].s>>a[0].v>>a[1].s>>a[1].v;
ll t1 = a[0].v*a[1].s;
ll t2 = a[1].v*a[0].s;
ll res = 0;
if(t1>=t2)
mx.s=a[0].s,mx.v=a[0].v,mn.s=a[1].s,mn.v=a[1].v;
else
mx.s=a[1].s,mx.v=a[1].v,mn.s=a[0].s,mn.v=a[0].v;
ll top = V/mn.s;
top = min(top,mx.s-1);
for(ll j=0;j<=top;j++)
{
ll tmp1=j*mn.v;
ll tmp2=(V-j*mn.s)/mx.s*mx.v;
res = max(res,tmp1+tmp2);
}
printf("Case #%d: %lld\n",i,res);
}
return 0;
}
这道题简化的是取低价值物品数量的范围。
首先,我们知道:
箱子的体积是V
每个物品1 的体积是 s1,价值是 v1。
每个物品2 的体积是 s2 ,价值是v2。
那么当有s2个物品1时,物品1的体积是s2 * s1,价值是 s2 * v1.
同理当有s1个物品2时,物品2的体积是s1 * s2,价值是 s1 * v2.
很显然这时 :物品1的体积 = 物品2的体积,而 物品1 的价值 不一定等于 物品2的价值。
我们假设相同体积下物品1的价值更高:
也就是说体积相同时,s2 * v1 > s1 * v2.
反过来说,当有s1个物品2时他的价值必然低于 s2个物品1,那么物品2的数量肯定是小于s1的。
也就是说,物品1尽可能的取,而物品2最多取s1-1个。
当然这不是最简化的,明显取最小公倍数lcm时物品2取得个数会更少(时间复杂度更低)。
边栏推荐
- China potato slicer market trend report, technical dynamic innovation and market forecast
- 数据在内存中的存储&载入内存,让程序运行起来
- Learning record: STM32F103 clock system overview working principle
- JS --- all basic knowledge of JS (I)
- ucorelab3
- Research Report on shell heater industry - market status analysis and development prospect forecast
- 力扣刷题记录
- 学习记录:STM32F103 时钟系统概述工作原理
- Cost accounting [14]
- 用C语言写网页游戏
猜你喜欢

学习记录:TIM—基本定时器

LeetCode#36. Effective Sudoku

Learning record: Tim - Basic timer

JS --- all knowledge of JS objects and built-in objects (III)

Stm32 dossiers d'apprentissage: saisie des applications

Learning record: understand systick system timer and write delay function

TCP的三次握手与四次挥手

VS2019初步使用

信息安全-威胁检测引擎-常见规则引擎底座性能比较

C语言必背代码大全
随机推荐
LeetCode#2062. Count vowel substrings in strings
Research Report on market supply and demand and strategy of China's earth drilling industry
Opencv learning log 15 count the number of solder joints and output
E. Breaking the Wall
Find 3-friendly Integers
Learning record: Tim - capacitive key detection
Opencv learning log 32 edge extraction
HDU - 6024 Building Shops(女生赛)
区间和------离散化
JS --- all basic knowledge of JS (I)
想应聘程序员,您的简历就该这样写【精华总结】
Cost accounting [13]
Stm32 dossiers d'apprentissage: saisie des applications
HDU-6025-Coprime Sequence(女生赛)
学习记录:使用STM32F1看门狗
ucorelab3
0-1背包問題(一)
用C语言写网页游戏
Cost accounting [21]
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China