C. Bus
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0is called a bus journey. In total, the bus must make k journeys.

The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

Input

The first line contains four integers abfk (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

Output

Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

Examples
input
6 9 2 4

output
4

input
6 10 2 4

output
2

input
6 5 4 3

output
-1

Note

In the first example the bus needs to refuel during each journey.

In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

数学问题;

因为他每次走的距离只有2种(开始和结束不算,可以特判);

一种是2*f,另一种是2*(a-f);

因为走2次就是两趟;

所以每次走的距离次数是k级别的;

然后只要贪心,走到最后没油的时候;

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;int a,b,c,d,f[1000008],flag,tot,k,ans;int main(){scanf("%d%d%d%d",&a,&b,&c,&d);f[1]=c;ans=0;tot=1; k=1; flag=0;while(k<=d){if(!flag) f[++tot]=2*(a-c); else f[++tot]=2*c;k++;flag=1-flag;}f[tot]=f[tot]/2;int i=1,res=b;while(i<=tot){if(f[i]<=res){res-=f[i];i++;}else{res=b; ans++;}if(f[i]>b){printf("-1");return 0;}}printf("%d",ans);
}

转载于:https://www.cnblogs.com/WQHui/p/7593837.html

Codeforce C. Bus相关推荐

  1. 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/ 本文出自方志朋的博客 转载请标明出处: Spr ...

  2. linux下bus、devices和platform的基础模型

    转自:http://blog.chinaunix.net/uid-20672257-id-3147337.html 一.kobject的定义: kobject是Linux2.6引入的设备管理机制,在内 ...

  3. Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    上一篇文章,留了一个悬念,Config Client 实现配置的实时更新,我们可以使用 /refresh 接口触发,如果所有客户端的配置的更改,都需要手动触发客户端 /refresh ,当服务越来越多 ...

  4. Spring Cloud第八篇:Spring Cloud Bus刷新配置

    Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...

  5. Linux-CAN Bus

    Linux-CPU与CAN设备 Linux下 CAN Bus协议的实现,有一部分是由硬件来实现,有一部分是软件实现. CAN总线基础和在linux下使用实战 CAN协议的实现,在不同平台上有不同的内核 ...

  6. java message bus_【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例...

    今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支 ...

  7. Vue教程3【使用Vue脚手架】render ref props minin scoped $emit $bus 消息订阅发布 动画

    npm全局安装 切换淘宝npm镜像 npm config set registry https://registry.npm.taobao.org全局安装 npm install -g @vue/cl ...

  8. Vue 组件通信之 Bus

    关于组件通信我相信小伙伴们肯定也都很熟悉,就不多说了,对组件通信还不熟悉的小伙伴移步这里. 在vue2.0中 $dispatch 和 $broadcast 已经被弃用.官方文档中给出的解释是: 因为基 ...

  9. ZOJ 2913 Bus Pass (近期的最远BFS HDU2377)

    题意  在全部城市中找一个中心满足这个中心到全部公交网站距离的最大值最小 输出最小距离和满足最小距离编号最小的中心 最基础的BFS  对每一个公交网站BFS  dis[i]表示编号为i的点到全部公交网 ...

最新文章

  1. pythonflask configlist.py_Python+Flask.0004.FLASK配置管理之三种方式加载外部配置
  2. Ubuntu安装Python模块时的报错
  3. 深入理解 Docker 架构 | 图文
  4. mysql多表查询练习_MySQL多表查询综合练习答案
  5. ASP.NET 学习日志
  6. 一文详解经纬度坐标与平面坐标转换
  7. 多变量微积分笔记5——梯度与方向导数
  8. 网络领域 ——《Adaptable Switch: A Heterogeneous Switch Architecture for Network-Centric Computing》
  9. RAD Studio 10.3.1 cannot initialize object parameter of type..........“
  10. 荣耀4a刷android 6,华为荣耀4a刷机教程_华为荣耀4a强刷官方系统包
  11. 几个期货基本面因子的研究
  12. 刘彬20000词汇01
  13. chrome浏览器多页签唯一关闭时自动注销
  14. seo每天的日常工作内容是哪些?
  15. 幸福婚庆策划网管理系统
  16. PDFlib 开发指南
  17. 微信小程序开发需要什么前提条件?
  18. python27读书笔记0.1
  19. tomacat出错_繁星漫天_新浪博客
  20. 印度尼西亚通过加密货币期货交易规则

热门文章

  1. mysql中mapping标签的作用_3种高效的Tags标签系统数据库设计方案分享
  2. python︱ collections模块(namedtuple/defaultdict/OrderedDict等)
  3. Java /Jsp 执行操作系统命令 windows/Linux
  4. 不同域名指向静态图片文件
  5. ES6中的高阶函数:如同 a = b = c 一样简单
  6. Mac搭建Nodejs+Express
  7. 【祈福】一句话让你的网页为灾区祈福(让网页变灰色)
  8. Eclipse 插件 在线安装 收集
  9. nodejs 批处理执行 app.js
  10. 重温Javascript继承机制