CodeForces_864_bus
2 seconds
256 megabytes
standard input
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.
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.
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.
6 9 2 4
4
6 10 2 4
2
6 5 4 3
-1
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的更多相关文章
随机推荐
- 细讲递归(recursion)
首先先对递归进行入门. 递归是以自相似的方式重复项目的过程.在编程语言中,如果程序允许您在同一函数内调用函数,则称其为函数的递归调用. 简而言之,递归就是函数的自身调用.可以看看下面的递归使用: vo ...
- DedeCMS织梦自定义图片字段调用出现{dede:img ..}
做站过程中碰到这样一个问题,找到解决办法收藏分享:为什么在首页用自定义列表调用出来的图片字段不是正确的图片地址,而是类似于: {dede:img text='' width='270' height= ...
- mybatis连接mysql数据库实现的jdbc功能
最近公司项目要使用myBatis,自己以前没有接触过,就在网上找到了一些资料研究了些.初步做出了基于myBatis连接mysql数据库的jdbc实现的功能. employee.java package ...
- Jersey实现文件上传下载
一 文件上传 使用ajaxFileUpload进行文件上传的前端处理.在ajaxFileupload.js中,针对服务端返回的类型增加text判断, //ajax文件上传 function ajaxF ...
- SQL Server 创建用户
增加角色 role_for_nc 1.exec sp_addrole 'role_for_nc'; 创建一个 SQL Server 登录名wlzx,密码为"123",默认数据库为 ...
- 【第一篇笔记】C# 全局容错,全局异常
网上找到两个方式,一个简单的只是做个记录,另一个能像QQ一样提交到后台. 方法一: static class Program { /// <summary> /// 应用程序的主入口点. ...
- 《Google软件测试之道》之学习笔记01
Google软件测试介绍 软件测试团队->工程生产力(Engineering Productivity) http://googletesting.blogspot.com/2011/01/ho ...
- jQuery 显示与隐藏 tab选项卡
方法一:使用display样式:block.none来控制文本的显示与隐藏 <div class="explain_text"> 移动互联网为企业提供了连接用户的新方式 ...
- 使用js来执行全屏
当用户按下F11事件,浏览器为触发自身全屏功能,这个过程我们一般是不可控制的,即使是监听了F11的键盘事件,退出全屏的时候,我们也捕捉不到退出全屏触发的事件.所以,我们就用程序自己去实现F11的功能, ...
- c语言函数指针的几种使用方式
1.直接定义函数指针赋值并使用. #include <stdio.h> int max(int x, int y) { if (x > y) return x; else retur ...