URAL 1607. Taxi

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;
}
时间: 2024-11-08 18:46:00

URAL 1607. Taxi的相关文章

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

Windows 10 1607(周年更新)后加入域无法使用WIndows Hello解决方法

一.问题现象 Windows 10客户端更新2016年8月3日的周年更新后,版本更新到了1607,加入域后(AD版本为Windows Server 2012 R2)Windows Hello设置为灰色,无法设置,提示"某些设置由你的组织来管理". 二.问题原因 由于Windows Server 2012 R2组策略中没有关于PIN登录的设置,Windows 10升级到1607版本后Windows 2012 R2默认是禁用PIN登录的.如果要启用Windows Hello功能,只需要通过

Windows下Mysql启动“服务名无效”及“系统错误1607”解决办法

1.服务名无效 输入net start mysql, 报错“服务名无效” 解决: 进入Mysql安装的bin目录,例如“C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin” 执行命令:mysqld -install 安装mysql服务 2.系统错误1607 输入net start mysql, 报错“系统错误1607” 解决: a.>bin下执行mysqladmin-u root -p shutdown,然后重新执行net start mysql

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn