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. PV、UV、VV,CV的含义

    其中VV和CV是播放类指标,PV和UV是浏览类指标. 1. 播放类指标 VV(Video View,播放数),是指在一个统计周期内,视频被打开的次数之和. CV(Connect Views,内容播放数 ...

  2. Mongodb~连接串的整理

    mongodb连接串可以分为普通开放的,带全局用户名和密码的,为指定数据库指定用户名密码的等. 普通开放连接 mongodb://localhost:27017 带全局用户密码的 mongodb:// ...

  3. PHP用mysql数据库存储session

    大部分使用php的人一旦应用到session都会使用cookie. cookie虽好可是它也会给我们带来一些隐患的. 隐患一:如果客户端机器的cookie一旦因病毒而失效了,那么session也就相当 ...

  4. php防止重复提交问题总结

    用户提交表单时可能因为网速的原因,或者网页被恶意刷新,致使同一条记录重复插入到数据库中,这是一个比较棘手的问题.我们可以从客户端和服务器端一起着手,设法避免同一表单的重复提交. 1.使用客户端脚本 提 ...

  5. JEECMS站群管理系统-- 首页的加载过程

    在浏览器中输入http://localhost:8080/jeecms,回车 首先进入配置文件web.xml, <context-param> <param-name>cont ...

  6. 新建mavent项目报错

    1.找到自己项目 项目名\.settings\org.eclipse.wst.common.project.facet.core.xml 将<installed facet="jst. ...

  7. 解决MyEclipse报errors running builder ‘javascript validator’ on project

    今天导入项目的时候,报了以下错误 MyEclipse测到功能代码变化(保存动作触发)就报错: errors running builder ‘javascript validator’ on proj ...

  8. 将BufferedImage转换为InputStream,亲测可用

    private static final Logger logger = Logger.getLogger(Demo.class); /** * 将BufferedImage转换为InputStrea ...

  9. 刚刚写的一个lua下解释csv的工具。

    csvtool = {} function csvtool:csv2table(filename) if type(filename) ~= "string" or filenam ...

  10. check_mk检测插件编写

    参考 Writing Checks (Introduction) Writing agent based checks The New Check API http://www2.steinkogle ...