题意:一辆车在一条路上行驶,给你路的总长度a,油箱的容量b,加油站在距离起点的距离f,以及需要走多少遍这条路k(注意:不是往返)

     问你最少加多少次油能走完。

Examples
Input
6 9 2 4
Output
4
Input
6 10 2 4
Output
2
Input
6 5 4 3
Output
-1

思路:把需要走的路程拉直来看,那么相邻两次经过加油站之间的路程为2*f或2*(a-f),再加上一些特判就好了。

代码:
#include<iostream>
#include<string.h>
using namespace std; long long a,b,f,k; int main(){
    cin>>a>>b>>f>>k;
    if(k==1){   //特判只要走一遍时的情况
        if(b>=a)cout<<0<<endl;
        else {
            if(b>=f&&b>=a-f)cout<<1<<endl;
            else cout<<-1<<endl;
        }
        return 0;
    }
    long long x=2*f,y=2*(a-f),sum=f,last=b-f,c=0;
    //x和y记录的是在两次经过加油站之间的路程的两种情况
    //sum记录的是已经走过了多少路程,从第一次经过加油站开始算
    //last记录的是油箱中所剩的油
    //c记录加油次数
    bool flag=1;
    int op=2;
    if(last<0){  //连加油站都走不到
        cout<<-1<<endl;
        return 0;
    }
    while(1){
        if(k*a-sum==f||k*a-sum==a-f){//在最后一次经过加油站时需要特判
            int w;
            if(k*a-sum==f)w=f;
            else w=a-f;
            if(last<w)c++;
            break;
        }
        if(op==1){   //op=1表示x这种情况
            if(last<x){
                last=b-x;
                c++;
            }
            else last-=x;
            sum+=x;
            op=2;
        }
        else {     //op=2表示y这种情况
            if(last<y){
                last=b-y;
                c++;
            }
            else last-=y;
            sum+=y;
            op=1;
        }
        if(last<0){//如果中间出现了油箱为负的情况,那么表示走不到
            flag=0;
            break;
        }
    }
    if(flag==0)cout<<-1<<endl;
    else cout<<c<<endl;
    return 0;
}

Codeforces Round #436 C. Bus的更多相关文章

  1. Codeforces Round #436 (Div. 2)【A、B、C、D、E】

    Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...

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

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

  3. Codeforces Round #436 (Div. 2) C. Bus

    http://codeforces.com/contest/864/problem/C 题意: 坐标轴上有x = 0和 x = a两点,汽车从0到a之后掉头返回,从a到0之后又掉头驶向a...从0到a ...

  4. Codeforces Round #436 (Div. 2) E. Fire

    http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...

  5. Codeforces Round #436 (Div. 2)

    http://codeforces.com/contest/864 第一次打cf的月赛-- A 题意:给你一个数列,问你能不能保证里面只有两种数且个数相等.2<=n<=100,1<= ...

  6. Codeforces Round #436 (Div. 2) D. Make a Permutation!

    http://codeforces.com/contest/864/problem/D 题意: 给出n和n个数(ai <= n),要求改变其中某些数,使得这n个数为1到n的一个排列,首先保证修改 ...

  7. Codeforces Round #436 (Div. 2) B. Polycarp and Letters

    http://codeforces.com/contest/864/problem/B 题意: 给出一个字符串,要求找到一个集合S,使得从S中选出的所有数,在这些数的位置上的字母全部为小写且是不同的字 ...

  8. Codeforces Round #436 (Div. 2)D. Make a Permutation! 模拟

    D. Make a Permutation! time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...

  9. Codeforces Round #436 (Div. 2) A,B,D

    A. Fair Game 题目链接:http://codeforces.com/contest/864/problem/A 水题 #include<iostream> #include&l ...

随机推荐

  1. Java中级开发工程师知识点归纳

    (一)Java 1.接口和抽象类的区别 ①抽象类里可以有构造方法,而接口内不能有构造方法. ②抽象类中可以有普通成员变量,而接口中不能有普通成员变量. ③抽象类中可以包含非抽象的普通方法,而接口中所有 ...

  2. php7.2 sqlsrv 扩展 ubuntu Homestead centOs

    PHP 7.2.9-1+ubuntu18.04.1 安装 sqlsrv 扩展 参考文章,感谢作者(建议先看,不看也可以) https://serverpilot.io/docs/how-to-inst ...

  3. 作业---修改haproxy配置文件

    #查询 f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8") ha ...

  4. Telegraf安装与介绍

    Telegraf 是什么? Telegraf 是一个用 Go 编写的代理程序,是收集和报告指标和数据的代理.可收集系统和服务的统计数据,并写入到 InfluxDB 数据库.Telegraf 具有内存占 ...

  5. 初学爬虫,关于scrapy

    对于anaconda与pycharm,因为在pycharm中一直下不成功tensorflow,所以后来又下了anaconda,pycharm就可以直接使用anaconda的编译器, 一直都很正常,但是 ...

  6. sas 9.4 sid 64bit 到期时间210804 带有EM

    PROC SETINIT RELEASE='9.4';SITEINFO NAME='NATIONAL PINGTUNG UNI OF SCIENCE&TECH'SITE=12001462 OS ...

  7. [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher 问题--MyEclipse设置JDK版本

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...

  8. 提取和匹配线特征的一个demo

    一.代码来源: https://github.com/drozdvadym/opencv_line_descriptor 二.依赖包:OpenCV 2.4.9 三.Matching的运行结果截图: 四 ...

  9. 记录一次JQuery 动态参数使用

    之前动态id 使用时时候一直是复制黏贴的,到自己实际使用的时候却毫无印象,这样不可取呀 $('#id')  取的是 id=‘id’ 的元素 $('#'+id)  才是取id为动态值的时候正确写法 记录 ...

  10. AES/CBC/PKCS7Padding加密方式

    在网上找了大半天资料,终于找到一个可以用的 public static class AES { // 算法名称 final static String KEY_ALGORITHM = "AE ...