[Codeforces 864C]Bus

Description

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 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.

Sample Input

6 9 2 4

Sample Output

4

HINT

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

题解

$a$是终点位置,$b$是油箱容量,$f$是加油站位置,$k$是要遍历的次数 $0$ ~ $a$或$a$ ~ $0$ 都算一次。

贪心策略是能不加油就不加油。我们记录在第$i$次遍历时在加油站不加油的油量$now$就可以了,模拟一遍就好。

 1 //It is made by Awson on 2017.9.29
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <cstdio>
10 #include <string>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Max(a, b) ((a) > (b) ? (a) : (b))
17 #define Min(a, b) ((a) < (b) ? (a) : (b))
18 #define sqr(x) ((x)*(x))
19 #define lowbit(x) ((x)&(-(x)))
20 using namespace std;
21 void read(int &x) {
22     char ch; bool flag = 0;
23     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == ‘-‘)) || 1); ch = getchar());
24     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
25     x *= 1-2*flag;
26 }
27
28 int a, b, f, k;
29
30 void work() {
31     read(a), read(b), read(f), read(k);
32     if (k==1 && (b < f || b < a-f)) {
33         printf("-1\n");
34         return;
35     }
36     if (k==2 && (b < f || b < 2*(a-f))) {
37         printf("-1\n");
38         return;
39     }
40     if (k>2 && (b < 2*f || b < 2*(a-f))) {
41         printf("-1\n");
42         return;
43     }
44     int now = b-f, ans = 0;
45     bool towards = 0;
46     for (int i = 1; i <= k; i++) {
47         int cost;
48         if (!towards) cost = a-f;
49         else cost = f;
50         if (i!=k) cost *= 2;
51         if (now >= cost) now -= cost;
52         else ans++, now = b-cost;
53         towards = !towards;
54     }
55     printf("%d\n", ans);
56 }
57 int main() {
58     work();
59     return 0;
60 }
时间: 2024-10-07 04:06:55

[Codeforces 864C]Bus的相关文章

CodeForces 711A Bus to Udayland (水题)

题意:给定一个n*4的矩阵,然后O表示空座位,X表示已经有人了,问你是不能找到一对相邻的座位,都是空的,并且前两个是一对,后两个是一对. 析:直接暴力找就行. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #includ

Codeforces 991E. Bus Number (DFS+排列组合)

解题思路 将每个数字出现的次数存在一个数组num[]中(与顺序无关). 将出现过的数字i从1到num[i]遍历.(i from 0 to 9) 得到要使用的数字次数数组a[]. 对于每一种a使用排列组合公式: ? ans += 上面那个公式.(每用一次这个公式对应一个a) 排列组合公式注解 减号左边表示的是sum个数字全排列并去重. 减号右边表示的是从a[0]中选出一个0当做第一个数字,并对其他数字全排列并去重. 抱歉,写的可能比较混乱,还有部分细节需要读者处理. 代码 #include<bit

[2016-04-09][codeforces][660][B][Seating On Bus]

时间:2016-04-09 23:29:47 星期六 题目编号:[2016-04-09][codeforces][660][B][Seating On Bus] 题目大意:按指定顺序入座,按指定顺序出座,问最后出座的顺序 分析:直接4个queue模拟一遍 #include<cstdio> #include<queue> using namespace std; queue<int> q[4]; int main(){ int n,m; scanf("%d%d&

Codeforces Round #436 (Div. 2) C. Bus

Codeforces Round #436 (Div. 2) C. Bus 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 pointx = a, immediately turns back and then moves to the point x = 0. After re

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters B. Bus of Characters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In the Bus of Characters there are nn rows of seat, each having 22

Codeforces 864 C Bus 思维

题目链接: http://codeforces.com/problemset/problem/864/C 题目描述: 输入a, b, f, k , 一段长度为a的路来回走, 中间f的地方有一个加油站, 油罐的容量为b, 问想要走b次这条路至少需要加多少次油 解题思路: 由于K <= 1e4, 所以将每次需要走路的连续序列构造出来再去遍历一遍这个序列看啥时候需要加油就可以了 代码: #include <iostream> #include <cstdio> #include &

Codeforces Round #481 (Div. 3) E. Bus Video System

E. Bus Video System Example 1 input 3 5 2 1 -3 output 3 Example 2 input 2 4 -1 1 output 4 Example 3 input 4 10 2 4 1 2 output 2 题目大意: 车上只会显示y-x(y是离开站时的人数,x时到站前的人数),问第一站的人数可以有几种 分析: 我们先分析下这个数学问题的式子,设a[i]=y[i]-x[i]--① x[i+1]=y[i],这是显然易见的 然后我们将①从1到n项累加

Codeforces Round #491 (Div. 2) E - Bus Number + 反思

E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong 就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法, 想了半天感觉时间来不及了就强行上了个java,结果时间来不及... 以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦. import java.math.Bi

Codeforces 435 A Queue on Bus Stop

题意:给出n队人坐车,车每次只能装载m人,并且同一队的人必须坐同一辆车,问最少需要多少辆车 自己写的时候想的是从前往后扫,看多少队的人的和小于m为同一辆车,再接着扫 不过写出来不对 后来发现把每一队的人装走之后,储存下这辆车还能装载的人数,每一次再判断 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6