CodeForces 625A Guest From the Past

本文探讨了在2084年利用最优策略以最低成本购买酸奶的方式,通过分析不同瓶型的价格、退款政策以及预算限制,提供了实现最大化酸奶消费量的解决方案。

Guest From the Past
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.

Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs a rubles, or in glass liter bottle, that costs b rubles. Also, you may return empty glass bottle and get c (c < b) rubles back, but you cannot return plastic bottles.

Kolya has n rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn't know how to act optimally and asks for your help.

Input

First line of the input contains a single integer n (1 ≤ n ≤ 1018) — the number of rubles Kolya has at the beginning.

Then follow three lines containing integers ab and c (1 ≤ a ≤ 10181 ≤ c < b ≤ 1018) — the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively.

Output

Print the only integer — maximum number of liters of kefir, that Kolya can drink.

Sample Input

Input
10
11
9
8
Output
2
Input
10
5
6
1
Output
2

Hint

In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.

In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir.


贪心,对于n>=b时并且a>b-c时取b-c更好,其次在考虑取a

因为在long long的范围内不能直接暴力,可以算公式。。。

代码很简单


代码如下

#include<cstdio>

using namespace std;

	long long n,a,b,c;
	long long ans=0;

int main()
{
	scanf("%I64d%I64d%I64d%I64d",&n,&a,&b,&c);
	if (n>=b && a>b-c)
	{
		ans=(n-b)/(b-c)+1;
		n-=((n-b)/(b-c)+1)*(b-c);
	}
	ans+=n/a;
	
	printf("%I64d\n",ans);
	
	return 0;
}


### Problem Overview The problem "Having Been a Treasurer in the Past, I Help Goblins Deceive" involves determining the minimum number of operations required to adjust a sum of money to a target value, given constraints on how much each operation can change the total. ### Problem Statement Given `n` (maximum number of operations), `k` (target value), and `p` (amount by which the total can be changed per operation), the goal is to compute the minimum number of operations needed to reach the target `k` using steps of size `p`. If it's impossible to reach the target within the given constraints, return `-1`. ### Solution Approach The solution involves a greedy strategy to determine the minimum number of steps required to reach the target. It also checks whether it's possible to achieve the target within the allowed number of operations. ### Key Observations - If the absolute value of the target `k` is greater than `n * p`, it's impossible to reach the target, and the result should be `-1`. - The number of steps required to reach the target can be calculated as the quotient of `|k| / p`, with an additional step if there's a remainder. - This approach ensures that the solution is efficient and adheres to the constraints of the problem. ### Code Implementation ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n, k, p; cin >> n >> k >> p; int res = abs(k) / p; if (abs(k) % p) { res += 1; } if (res > n) { cout << "-1" << endl; } else { cout << res << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; } ``` ### Explanation of the Code - The code reads multiple test cases and processes each one individually. - For each test case, it calculates the minimum number of operations required to reach the target `k` using steps of size `p`. - If the number of operations exceeds `n`, it outputs `-1` to indicate that the target is unreachable. - Otherwise, it outputs the computed number of operations[^2]. ### Example Scenarios Consider the following example: - Input: `n = 5`, `k = 12`, `p = 3` - Output: `4` (since `12 / 3 = 4`) Another example: - Input: `n = 3`, `k = 10`, `p = 3` - Output: `-1` (since `10 / 3 = 3.33`, and `4` operations are needed, which exceeds `n = 3`) ### Time and Space Complexity - **Time Complexity**: `O(1)` per test case, as the computation involves simple arithmetic operations. - **Space Complexity**: `O(1)`, as no additional space is used beyond the input variables. ### Conclusion This problem demonstrates the use of greedy algorithms to solve a practical problem in programming competitions. The approach is both efficient and straightforward, making it suitable for time-constrained environments like contests.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值