URAL 1607. Taxi

48 篇文章 0 订阅

1607. Taxi

Time limit: 0.5 second
Memory limit: 64 MB
Petr likes going by taxi. For him, it is not only the pleasure of a fast and comfortable ride, but also the opportunity to bargain with the driver over the fare. The bargaining between Petr and taxi drivers always follows the same scheme:
— To the airport! I pay 150 roubles.
— No, I won't take you for 150. Let's go for 1000.
— Are you crazy? I haven't got that much on me! Ok, let it be 200.
— Are you laughing? I agree to 900.
— Well, I'll give 250.
— Guy, do you know how much gas is? Pay 800 and off we go!

Such a dialog continues until they agree on the fare. Petr always increases his amount by the same number, and the taxi driver decreases it in the same way. The driver would not ask a sum that is less than that offered by Petr. In this case, he will agree with Petr's offer. Petr will act similarly.

Input

The single input line contains four integer numbers: the initial Petr's offer  a, Petr's raise to his offer  b, the initial fare required by the driver  c, and the driver's reduction of his fare  d; 1 ≤  abcd ≤ 10000 .

Output

Output the amount of money that Petr will pay for the ride.

Sample

input output
150 50 1000 100
450




解析:很关键的一句话是上文中的红色标记出来的要求,现实情况也是,他们两个一个加价,一个减价,当价格再加减一次就满足要求时,乘客只需要付给当前司机要求的价格与下一轮乘客要出的价格之间的最小值即可,当然,当初始时乘客给的价格就不小于司机要求的价格时,直接就交易了。



AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;

int  main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    int a, b, c, d;
    while(scanf("%d%d%d%d", &a, &b, &c, &d)==4){
        int ans = 0;
        for(int i=0; ; i++){
            if(a + b * i >= c - d * i){
                if(i) ans = min(a + b * i,  c - d * (i - 1));   //贪心付两者的最小值
                else ans = a;                  //初始就满足要求,直接成交
                break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值