您的位置:首页 > 产品设计 > UI/UE

Codeforces 625 . A Guest From the Past

2016-07-08 22:46 417 查看
    集训第二天,状态有所回升。

    个人赛中遇到一题,当时ac了,比完整理时仔细想想还没有完全理解透彻,遂彻底整理一下思路。

    见题目:

A. Guest From the Past

time limit per test
 1 second

memory limit per test
 256 megabytes

input
 standard input

output
 standard output

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 a, b and c (1 ≤ a ≤ 1018, 1 ≤ 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 test(s)

input
10
11
9
8


output
2


input
10
5
6
1


output
2


Note

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.

    英文题,看懂真心是最难的一关……

    题目的意思是:一个人去买酒,买塑料瓶的花费a每瓶,买玻璃瓶的花费b每瓶。买好之后获得返现c元,求最终的能买到的最多的酒的瓶数。

    很容易考虑到在a<b-c的情况下,全部买塑料瓶即可,在b-c<a的情况下,先尽可能地去买玻璃瓶剩下的钱买塑料瓶。贪心准则很容易理解。但该题需要注意的一点是买玻璃瓶的时候必须时刻确保剩下的钱能够买再买一瓶才能将价格算作b-c一瓶,如果单纯的是以n/(b-c)的话,以第一个案例来说,结果应该是10显然结果是错的,第一个案例的运算过程是先买一瓶玻璃瓶,剩余1元返现8元后剩余9元后再买一瓶,返现8元后结束。所以需要先将买b的钱匀出来,才能作后续的运算,这样确保(n-b)/(b-c)得到的每一瓶都能够买得到,剩下的b和运算所得余数的和还能够买一瓶,其价格一样是b-c,最后再用剩下的钱买a即可。该题容易错的地方在预留一瓶这一点上,理解运算过程后很容易写出代码。见AC代码:

#include <stdio.h>
int main()
{
long long n,a,b,c,res=0;
scanf("%I64d %I64d %I64d %I64d",&n,&a,&b,&c);
if(n>=b&&b-c<a)
{
res=(n-b)/(b-c)+1;
n-=res*(b-c);
}
res+=n/a;
printf("%I64d\n",res);
return 0;
}
    比完网上查找思路的时候,好多大神的眼中这都是水题,看来眼界还是不够宽广,唯有多加练习。勤能补拙,唯有奋斗。

    特记下,以备后日回顾。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: