• Codeforces Round #436 (Div. 2)C. Bus 模拟


    C. Bus
    time limit per test:
    2 seconds
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.

    The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

    There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

    What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

    Input

    The first line contains four integers abfk (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

    Output

    Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

    Examples
    input
    6 9 2 4
    output
    4
    input
    6 10 2 4
    output
    2
    input
    6 5 4 3
    output
    -1
    Note

    In the first example the bus needs to refuel during each journey.

    In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

    In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

     题目链接:http://codeforces.com/contest/864/problem/C

    题意:有一条长度为a的道路,在距离起点k的地方有一个加油站,每次加油可以把有加满,现在有一辆容量为b的车没走一单位距离耗费1单位的有,现在要在这条路上开k趟,去是一趟,回来也是一趟,问最少需要加几次油。

    思路:是否需要在本加油站加油,是看车能否到达下一个加油站,如果能,本加油站不加油;如果不能,本加油站需要加油。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<map>
    #include<set>
    #include<bitset>
    using namespace std;
    #define bug(x) cout<<"bug"<<X<<endl;
    #define PI acos(-1.0)
    #define eps 1e-8
    typedef long long ll;
    typedef pair<int,int > P;
    int main()
    {
        ll a,b,f;
        int k;
        scanf("%lld%lld%lld%d",&a,&b,&f,&k);
        int ans=0;
        ll sum=b;
        for(int i=1; i<=k; i++)
        {
            if(i%2) sum-=f;
            else sum-=a-f;
            if(sum<0) return 0*printf("-1
    ");
            if(i<k)
            {
                if(i%2==1&&sum-2*(a-f)<0) sum=b,ans++;
                else if(i%2==0&&sum-2*f<0) sum=b,ans++;
            }
            else
            {
                if(i%2==1&&sum-(a-f)<0) sum=b,ans++;
                else if(i%2==0&&sum-f<0) sum=b,ans++;
            }
            if(i%2) sum-=a-f;
            else sum-=f;
            if(sum<0) return 0*printf("-1
    ");
        }
        printf("%d
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    Scalaz(3)- 基础篇:函数概括化-Generalizing Functions
    Scalaz(2)- 基础篇:随意多态-typeclass, ad-hoc polymorphism
    Scalaz(1)- 基础篇:隐式转换解析策略-Implicit resolution
    Scalaz(0)
    泛函编程(38)-泛函Stream IO:IO Process in action
    泛函编程(37)-泛函Stream IO:通用的IO处理过程-Free Process
    泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink
    泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
    javascript基础
    jsp联合javascript操作html
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7595606.html
Copyright © 2020-2023  润新知