题意:一辆车在一条路上行驶,给你路的总长度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. js任意数组按下标相加

    let a=[1,2,3], b=[4,5,6]; let s = a.map(function(v, i) { return v + b[i]; }); console.log(s);

  2. [UE4]线性插值Lerp

  3. Unity Graphics(一):选择一个光照系统

    原文链接 Choosing a Lighting Technique https://unity3d.com/learn/tutorials/topics/graphics/choosing-ligh ...

  4. 权限模型AGDLP

    关于权限模型,认真学习下AD+FS应用在企业中,使用AGDLP模型,即可. AD是微软最牛逼的设计之一.

  5. Java垃圾回收(整理)

    Java垃圾回收 Garbage Collection:GC: 什么样的对象才是垃圾?怎样判断一个对象引用是不是垃圾? 垃圾回收算法:Mark-Sweep(标记-清除)算法,Copying(复制)算法 ...

  6. uniapp仿h5+fire自定义事件触发监听

    仿h5+fire自定义事件触发监听 uni-app调用 event.js 源码记录(点击查看) 1.js下载地址 [event.js](https://ext.dcloud.net.cn/plugin ...

  7. Java 高级框架——Mybatis(一)

    一, SQl复习 a,数据库SQL命令 创建数据库并指定编码 Create database 数据库名 default character set utf8 create database ssm d ...

  8. React Native仿京东客户端实现(首页 分类 发现 购物车 我的)五个Tab导航页面

    1.首先创建  这几个文件 myths-Mac:JdApp myth$ yarn add react-native-tab-navigator 2.各个文件完整代码 1)CartPage.js imp ...

  9. Webservice 从客户端中检测到有潜在危险的 request.form值[解决方法]

    <system.web> <httpRuntime requestValidationMode="2.0" /> <pages validateReq ...

  10. PHP语言入门的学习方法十要素

    对于PHP程序设计语言来说.每个人的学习方式不同,写这篇文章的目的是分享一下自己的学习过程,仅供参考,不要一味的用别人的学习方法,找对自己有用的学习方式.经常在某些论坛和QQ群里看到一些朋友会问“怎样 ...