[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 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.
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$就可以了,模拟一遍就好。
//It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int a, b, f, k; void work() {
read(a), read(b), read(f), read(k);
if (k== && (b < f || b < a-f)) {
printf("-1\n");
return;
}
if (k== && (b < f || b < *(a-f))) {
printf("-1\n");
return;
}
if (k> && (b < *f || b < *(a-f))) {
printf("-1\n");
return;
}
int now = b-f, ans = ;
bool towards = ;
for (int i = ; i <= k; i++) {
int cost;
if (!towards) cost = a-f;
else cost = f;
if (i!=k) cost *= ;
if (now >= cost) now -= cost;
else ans++, now = b-cost;
towards = !towards;
}
printf("%d\n", ans);
}
int main() {
work();
return ;
}
[Codeforces 864C]Bus的更多相关文章
- 【模拟】Codeforces 711A Bus to Udayland
题目链接: http://codeforces.com/problemset/problem/711/A 题目大意: N个字符串,每个字符串5位,找到第一个出现两个OO的并改成++输出YES和改后字符 ...
- Codeforces G. Bus Number(dfs排列)
题目描述: Bus Number time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 711A Bus to Udayland (水题)
题意:给定一个n*4的矩阵,然后O表示空座位,X表示已经有人了,问你是不能找到一对相邻的座位,都是空的,并且前两个是一对,后两个是一对. 析:直接暴力找就行. 代码如下: #pragma commen ...
- CodeForces 711A Bus to Udayland
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- codeforces 982B Bus of Characters
题意: 有n排座位,每排有两个座位,每排座位的宽度都不一样. 有2 * n个人要上车,如果是内向的人,那么它会选择一排两个都是空位并且宽度最小的一排去坐: 如果是外向的人,会选择一排座位已经有人坐的, ...
- Codeforces 991E. Bus Number (DFS+排列组合)
解题思路 将每个数字出现的次数存在一个数组num[]中(与顺序无关). 将出现过的数字i从1到num[i]遍历.(i from 0 to 9) 得到要使用的数字次数数组a[]. 对于每一种a使用排列组 ...
- Codeforces Round #436 (Div. 2)C. Bus 模拟
C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...
- Codeforces Round #369 (Div. 2) A. Bus to Udayland 水题
A. Bus to Udayland 题目连接: http://www.codeforces.com/contest/711/problem/A Description ZS the Coder an ...
- Educational Codeforces Round 11 B. Seating On Bus 水题
B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...
随机推荐
- CountDownLatch 源码解析—— countDown()
上一篇文章从源码层面说了一下CountDownLatch 中 await() 的原理.这篇文章说一下countDown() . public void countDown() { //CountDow ...
- 2018.3.29 div格式设置
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- 第2次作业:软件分析之Steam
1. 作业内容 1.1 介绍产品相关信息 你选择的产品是? 我选择的分析的软件为STEAM 为什么选择该产品作为分析? 在上述列表中的产品,除了王者荣耀,其他几项都是平时我使用较为频繁的软件,所以最初 ...
- 网络1711班 C语言第七次作业批改总结
网络1711班 C语言第七次作业批改总结 1.本次作业评分细则 1.1 基本要求(1分) 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 泛泛而谈(最多七分) 1 ...
- 201621123060《JAVA程序设计》第十一周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...
- C语言——第0次作业(二)
1.翻阅邹欣老师的关于师生关系博客,并回答下列问题,每个问题的答案不少于500字: 1.最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助? 在现代软件工程讲义 0 ...
- android context获取目录详解
获取 sqlite系统数据库路径 方式1: ApkInfo apkInfo = new ResourceUtil(context).getApkInfo(); APP_PATH = new Strin ...
- android批量打包
http://blog.csdn.net/johnny901114/article/details/48714849
- 一个CSS简单入门网站
讲的知识简单明了,很实用: http://zh.learnlayout.com/
- 【原创】Webpack构建中hash的优化
背景: SPA的vue应用,采用webpack2构建,打包入口为main.js 输出:main模块打包成app.js,公共lib打包成vendor.js,公共样式打包成app.css,运行时依赖打包成 ...