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 = 0 is 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 a, b, f, k (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 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

题意:一辆公交车在一段路上往返跑,往或返都算一次journey,路上有一个加油站f,能否完成k次journey。

  思路当然是模拟了,一开始我想到的是记录往或着返的时候   油(此处用now代替)是否是够的。假举了多种情况没有找到合适的标准来计算次数。然后又重新以是否需要加油为标准判断次数。     赛后补题发现,我开始的思路不算错,只记录x=0,和x=a的now来进行模拟。并且判断是否是最后一次旅途就可以解出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100010
using namespace std;
int main()
{
int a,b,f,k;
while(~scanf("%d%d%d%d",&a,&b,&f,&k))
{
int d=0,ans=0;
int now;
now=b-f;
int to=1;
for(int i=0;i<k&&now>=0;i++){
if(to){
if(i==k-1){
if(now<a-f){
now=b-(a-f);
ans++;
}
}
else if(now>=2*(a-f)){
now-=2*(a-f);
}
else {
now=b-2*(a-f);
ans++;
}
to=0;
}
else{
if(i==k-1){
if(now<f){
now=b-f;
ans++;
}
}
else if(now>=2*f){
now-=2*f;
}
else{
now=b-2*f;
ans++;
}
to=1;
}
}
if(now<0){
puts("-1");
}
else{
printf("%d\n",ans);
} }
}

  

CodeForces_864_bus的更多相关文章

随机推荐

  1. (转)Linux命令之md5sum

    Linux命令之md5sum  原文:https://www.cnblogs.com/zhuxiaohou110908/p/5786893.html 1. 背景 在网络传输.设备之间转存.复制大文件等 ...

  2. php 关于时间的函数

    //返回1970年1月1日零点以来的秒数.    //定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. time(); ...

  3. stl::iterator汇总

    STL——iterator 一.概述Iterator(迭代器)模式又称Cursor(游标)模式, 根据STL中的分类,iterator包括:Input Iterator:只能单步向前迭代元素,不允许修 ...

  4. Win2D 官方文章系列翻译 - 像素格式

    本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-pixel-formats/ DirectXPixelFormat 枚举 包含了 Direct3D 和 DXG ...

  5. Could not find or load main class Hello

    在 linux 下写了一个非常简单的 Hello World 程序,编译正常,运行报错:Error: Could not find or load main class Hello 这是由于 CLAS ...

  6. flexbox预习

    创建一个flexbox: .flex-container{ display:flex; } flex-direction:  column;//将flex排成一列 flex-direction: co ...

  7. int **a 和 int (*a)[]的区别

    关于理论知识隔壁们的教程说的很详细了我就不多赘述了.我这边主要贴一段代码来看看这两种东西使用上的区别到底在哪. #include <stdio.h> int main(int argc, ...

  8. Linux读取NTFS类型数据盘

    Windows的文件系统通常使用NTFS或者FAT32格式,而Linux的文件系统格式通常是EXT系列,请参考下面方法: 1) 在Linux系统上使用以下命令安装ntfsprogs软件使得Linux能 ...

  9. [转]Android解析json数据

    1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...

  10. CODESOFT条码设计软件如何隐藏数据源方法

    作为强大的条码标签设计软件,用户在用CODESOFT设计条码标签时,有时需要根据实际情况,将条码数据源隐藏,也就是使设计与打印出来的条形码下不带有数据.那么这要怎么在CODESOFT中实现呢?下面,小 ...