您的位置:首页 > 其它

Codeforces 864 C Bus

2017-10-23 11:40 316 查看
题目地址

题意:有一条路长度为a,在路中坐标为f的位置有一个加油站,车子的油箱能装下b升汽油,每公里用一升汽油,问你重复k次路程最少要加几次油,能不能重复k次路程,(一次路程的定义是0->a或者a->0)

思路:我们每次判断油够不够就好了,不够就加油,当油箱满了不能跑那么多路的话就是输出-1,直接模拟就好了,唯一要注意的就是k=1和k=2的情况,特判下就好了(画图就能理解)。

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#define N 100010
#define M 2000010//双倍
#define LL __int64
#define inf 0x3f3f3f3f
#define lson l,mid,ans<<1
#define rson mid+1,r,ans<<1|1
#define getMid (l+r)>>1
#define movel ans<<1
#define mover ans<<1|1
using namespace std;
const LL mod = 1000000007;
const double eps = 0.001;
int main() {
cin.sync_with_stdio(false);
int a, b, f, k;
int cnt, ans;
while (cin >> a >> b >> f >> k) {
cnt = 0;
ans = b;
if (k > 2) {
if (f * 2 > b || (a - f) * 2 > b) {
cout << -1 << endl;
continue;
}
}
else if (k == 1) {
if (f > b || (a - f) > b) {
cout << -1 << endl;
continue;
}
}
else {
if (f > b || (a - f) * 2 > b) {
cout << -1 << endl;

c548
continue;
}
}
ans -= f;
for (int i = 1; i < k; i++) {
if (i % 2) {
if (ans >= (a - f) * 2) {
ans -= (a - f) * 2;
}
else {
cnt++;
ans = b - (a - f) * 2;;
}
}
else {
if (ans >= f * 2) {
ans -= f * 2;
}
else {
cnt++;
ans = b - f * 2;;
}
}
}
if (k % 2 == 0 && ans < f) {
cnt++;
}
if (k % 2 == 1 && ans < a - f) {
cnt++;
}
cout << cnt << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: