题意:一辆车在一条路上行驶,给你路的总长度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. php7 使用dom动态生成xml文档

    <?php $dom = new DomDocument('1.0','gb2312'); //创建DOM对象 $store = $dom->createElement('store'); ...

  2. [例子] nginx负载均衡搭建及测试

    一.Nginx + Tomcat 负载均衡测试(负载均衡+部分静态图片处理) 环境说明:  nginx+tomcat @ubuntu ok 首先你得有一个Ubuntu或者什么其他的linux. 安装j ...

  3. GitLab配置后收取不到邮件问题

    一.root权限下,修改gitlab的配置文件:/etc/gitlab/gitlab.rb gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_ ...

  4. 读完这一篇,字符串格式化界的“白富美”(f-strings)抱回家!

    f-strings 从Python 3.6开始,新引入了一种字符串格式化方法,称为“格式化字符串常量”(formatted string literal),简称f-strings.相比于%.str.f ...

  5. Error in loadNamespace 的解决之道

    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])   在构建比较复杂的环 ...

  6. Docker安装使用battery historian

    apt-get insatll docker.io battery historian ubuntu下使用 首先要确保是google浏览器,然后用命令行 google-chrome --proxy-s ...

  7. json2.js JSON解析程序

    源码: /* http://www.JSON.org/json2.js 2010-03-20 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE ...

  8. Python量化分析,计算KDJ

    Python: v3.6 Pandas: v0.23.4 使用以下方法计算与国内财经软件显示一致 low_list = df['最低价'].rolling(9, min_periods=9).min( ...

  9. 学习笔记之English

    雅思听力地图题的常用短语 - 无忧机经预测 https://mp.weixin.qq.com/s/VmV3L2METymtjMWHY2fNiA 雅思听力租房的那些事儿 - 北京市海淀区环球雅思 htt ...

  10. Unity 代码组件获取和使用、Resources加载、OnGUI、Time、Mathf、PlayerPref

    1.     游戏物体组件获取.添加组件(重要) 作业分析: 子弹生成:坦克生成----->坦克控制类里生成子弹 子弹飞行:子弹自己飞,不能通过坦克控制类进行管理: 获取代码组件,设置子弹速度: ...