题意:一辆车在一条路上行驶,给你路的总长度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. Convert.ChangeType不能处理Nullable类型的解决办法(转)

    https://www.cnblogs.com/patrickyu/p/3211115.html 在做一个ORMapping功能的时候发现,Convert.ChangeType不能处理nullable ...

  2. AttributePriority

    还有AttributePriority,我们可以设置编译时优先级.如果我们对目标标记了多个aspect,这样postsharp就不确定注入先后顺序,这样不能确保正确性,在vs编译时候我们会看见警告:T ...

  3. C#源码发送简单的HTTP请求

    如下代码内容是关于C#发送简单的HTTP请求的代码,应该能对大家有用处. using System;using System.Collections.Generic;using System.Linq ...

  4. C3D视频特征提取

    一.部署 1. 先把项目Clone下来 git clone https://github.com/jfzhang95/pytorch-video-recognition.git 2. 安装环境: Py ...

  5. 朴素贝叶斯法(naive Bayes algorithm)

    对于给定的训练数据集,朴素贝叶斯法首先基于iid假设学习输入/输出的联合分布:然后基于此模型,对给定的输入x,利用贝叶斯定理求出后验概率最大的输出y. 一.目标 设输入空间是n维向量的集合,输出空间为 ...

  6. 02-Tomcat服务器安装

    由于使用的是解压绿色版,所以解压就可以使用,如下图显示 启动成功

  7. Egret飞行模拟-开发记录01

    1.项目结构简介 1.1 index.html:应用入口文件,我们可以在这里面配置项目的旋转缩放模式背景颜色等. 1.2 egretProperties.json:这个文件里面进行项目配置,包括模块和 ...

  8. Resharper快捷键汇总

    编辑Ctrl + Space 代码完成 Ctrl + Shift + Space代码完成Ctrl + Alt + Space代码完成Ctrl + P 显示参数信息Alt + Insert 生成构造函数 ...

  9. 学习MeteoInfo二次开发教程(十)

    1.复制文件cloud_1009271330.000,I-01.pal 2.改: GridData cloud = aDataInfo.GetGridData(""); 为: Gr ...

  10. [STM32F103]DMA原理

    DMA配置程序过程 使能DMA时钟 a) RCC_AHBPeriphClockCmd(); 初始化DMA通道参数 a) DMA_Init(); 使能串口DMA发送,串口DMA使能函数: a) USAR ...