ZeptoLab Code Rush 2015 C题Om Nom and Candies

本文介绍了一个关于糖果选择的问题:如何在不超过重量限制的情况下获得最大的幸福感。通过对比不同糖果的性价比,采取逐步调整策略来求解最优解。

C. Om Nom and Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?

One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.

Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than Cgrams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.

Input

The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).

Output

Print a single integer — the maximum number of joy units that Om Nom can get.

Sample test(s)
input
10 3 5 2 3
output
16
Note

In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.



题意大概就是一个怪兽吃糖果,有两种糖果,然后每种糖果都有其重量和幸福感(吃完后得到的幸福感),然后这个怪兽要求在重量不超过C的情况下使幸福感最大,糖果数量没有限制,问最大的幸福感是多少。

我的做法就是先吃性价比最高的糖。看还剩下多少重量可以吃另外一种糖。如果重量还有剩余,然后逐步减少性价比最高的糖的数量,去吃另一种糖,直到重量剩余为0或者性价比最高的糖的数量为0.就KO了。

#include<limits>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

#define LL long long
#define eps 1e-10
#define pi acos(-1)
#define INF 0x7fffffff
#define delta 0.98 //模拟退火递增变量
using namespace std;
int dcmp(double x){
	if (fabs(x)<=eps) return 0;
	if (x>0) return 1;
	return -1;
}
int main(){
	LL c,hr,hb,wr,wb;
	scanf("%I64d%I64d%I64d%I64d%I64d",&c,&hr,&hb,&wr,&wb);
	if (dcmp(hb*1.0/wb-hr*1.0/wr)<0){ //保证蓝色是最大的 
		swap(hr,hb);
		swap(wr,wb);
	}
	LL l=c;
	LL k=l/wb;
	LL ans=k*hb;
	l=c-k*wb;
	if (l>=wr){
		ans+=(l/wr)*hr;
		l-=l/wr*wr;
	}
	if (hr<=hb && wr>=wb)
		cout<<ans<<endl;
	else{
		LL gg=ans;
		while (k>0 && l){
			k--;
			l+=wb;
			gg-=hb;
			if (l>=wr){
				gg+=l/wr*hr;
				l-=l/wr*wr;
			}
			if (gg>ans) ans=gg;
		}
		cout<<ans<<endl;
	}
	return 0;
}


内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值