题意:一辆车在一条路上行驶,给你路的总长度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. mplayer用法收集【转】

    转自:https://blog.csdn.net/wylhistory/article/details/4816653 1,录音: mplayer mms://202.***.***.***/test ...

  2. Excel与Google Sheets中实现线性规划求解

    很久没更新过APS系列文章了,这段时间项目工作确实非常紧,所以只能抽点时间学习一下运筹学的入门知识,算是为以后的APS项目积累点基础.看了一些运筹学的书(都是科普级别的)发现原来我目前面对的很多排产. ...

  3. 【rabbitmq】rabbitmq集群环境搭建

    安装rabbitmq-server 总共有3台虚拟机,都安装有rabbitmq服务,安装过程可参考: [rabbitmq]Centos7 下安装rabbitmq 创建用户和vhost 说明: 此步骤不 ...

  4. DOM知识点总结

    今天简单整理了一下js三部曲之DOM部分的内容,二话不说先上笔记: 1.什么是DOM? Document Object Model,即文档对象模型,它是让JavaScript能够操作html和xml的 ...

  5. linux dd命令 创造一个文件

    创造一个1G的文件 dd if=/dev/zero of=/nod/tmp/test bs=1M count=1024 创造一个1T的文件 [root@oracledg tmp]# dd if=/de ...

  6. navicat 导入execl失败

    在使用navicat导入execl是遇到了如下图的错误 在更换多个版本的navicat后问题依然如故. 解决办法; 1.打开需要导入的execl 2.安装一个AccessDatabaseEngine_ ...

  7. Best Practices and Recommendations for RAC databases with SGA size over 100GB (文档 ID 1619155.1)

    Best Practices and Recommendations for RAC databases with SGA size over 100GB (文档 ID 1619155.1) APPL ...

  8. Linux 版 SecureCRT 界面变为 Windows 2000 风格的解决办法

    SecureCRT 是一款非常好用的远程终端连接软件,支持 Windows.Linux.macOS 全平台.由于现在工作平台主要在 Linux 系统上,SecureCRT 也是必备软件.一开始安装的是 ...

  9. 64 位 Windows 平台开发注意要点之注册表重定向

    Window 系统错误代码 ERROR_SUCCESS,本博客中一律使用 NO_ERROR 代替.虽然 ERROR_SUCCESS 与 NO_ERROR 是完全等价的,都代表成功,但是后者却和其他错误 ...

  10. select拼接

    //if (Data1[i].MisFunId == 1) //{ // if (Data1[i].Flag == true) // { // var t = Data1[i].MisFunId; / ...