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[],flag,tot,k,ans; int main(){
scanf("%d%d%d%d",&a,&b,&c,&d);
f[]=c;
ans=;
tot=; k=; flag=;
while(k<=d){
if(!flag) f[++tot]=*(a-c); else f[++tot]=*c;
k++;
flag=-flag;
}
f[tot]=f[tot]/;
int i=,res=b;
while(i<=tot){
if(f[i]<=res){
res-=f[i];
i++;
}
else{
res=b; ans++;
}
if(f[i]>b){
printf("-1");
return ;
}
}
printf("%d",ans);
}

Codeforce C. Bus的更多相关文章

  1. 设备模型(device-model)之平台总线(bus),驱动(driver),设备(device)

    关于关于驱动设备模型相关概念请参考<Linux Device Drivers>等相关书籍,和内核源码目录...\Documentation\driver-model 简单来说总线(bus) ...

  2. Matlab中使用脚本和xml文件自动生成bus模块

    帮一个老师写的小工具 在一个大工程中需要很多bus来组织信号,而为了规范接口,需要定义很多BusObject,用Matlab语言手写这些BusObject比较费工夫 所以用xml配置文件来写,也便于更 ...

  3. bzoj 1537: [POI2005]Aut- The Bus 线段树

    bzoj 1537: [POI2005]Aut- The Bus 先把坐标离散化 设f[i][j]表示从(1,1)走到(i,j)的最优解 这样直接dp::: f[i][j] = max{f[i-1][ ...

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

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

  5. ACE bus

    ACE bus增加的内容: 1):5状态的cache model 2):关于coherency的additional signal 3):两个cache master访问shared cache的ad ...

  6. 利用Service bus中的queue中转消息

    有需求就有对策就有市场. 由于公司global的policy,导致对公司外发邮件的service必须要绑定到固定的ip地址,所以别的程序需要调用发邮件程序时,问题就来了,如何在azure上跨servi ...

  7. c程序中出现segment error 和 bus error 的原因

    在c程序中,经常会遇到段错误(segment error)和总线错误(bus error),这两种问题出现的原因可能如下 段错误: 对一个NULL指针解引用. 访问程序进程以外的内存空间. 实际上,第 ...

  8. Azure Service Bus 中的身份验证方式 Shared Access Signature

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  9. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

随机推荐

  1. mysql数据库快速入门(1)

    1.数据库操作 1.1.连接mysql服务器 mysql -u root( 用户名 ) -p 1.2.退出mysql命令提示窗 exit 1.3.查看版本 SELECT VERSION(); 1.4. ...

  2. CSS基础知识(颜色、伪类、盒子模型)

    6.设置颜色单位 L    普通英文单词 {color : 属性值red;} 此方法简单,便捷.但设置的颜色在不同浏览器中,可能显示的颜色出现差异 * 三原色 - 红.绿.蓝 L   颜色的八进制方式 ...

  3. 通俗的解释JAVA wait/notify机制

    生活中,我们常遇到需要等待的场景,例如去银行办事,在没轮到自己之前需要一直等待,但是如果需要自己每隔几秒钟就去柜台前看看状况,那肯定是种非常低效和令人恼火的体验.而实际的情况是,接待员会让您拿个号,说 ...

  4. UESTC1599-wtmsb-优先队列

    wtmsb Time Limit: 1000/100MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others) 这天,AutSky_Jad ...

  5. post请求测试

    也可以用下图来指定测试参数

  6. Ajax及异步操作

    之前我们使用的是jQuery的Ajax,这是一种极为便捷的Ajax操作方式,但是我们还需要对Ajax技术进行进一步的了解. <input type="text" id=&qu ...

  7. HDU 2119 Matrix

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2119 解题思路: 处理数据,使用公式最小点覆盖数=最大匹配数,使用匈牙利算法求二分图最大匹配即可. ...

  8. ZOJ 1203 Swordfish

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  9. Spark算子--SortBy

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/39edcbb10b5076599c0e5609b7136b88.html SortBy--Transforma ...

  10. VisualSFM+PMVS生成稠密点云

    利用相机拍摄一个场景不同角度的图片,使用VisualSFM能够得到稀疏点云,如果想要得到稠密点云,可以在VisualSFM中加入PMVS的应用程序,PMVS会作为一个插件运行将稀疏点云插成稠密的点云. ...