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 returning to the point x = 0 it immediately goes back to the pointx = 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 pointx = 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 5liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.
mycode:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main(){
int a,b,f,k;
//while(scanf("%d%d%d%d",&a,&b,&f,&k)){
scanf("%d%d%d%d",&a,&b,&f,&k);
int s = b;
int fg = 1;
int count = 0;
int ok = 1;
int i,tmp;
for(i=0;i<k;i++){
if(i==0){
s -= f;
if(s<0){
ok = 0;
break;
}
}
else if(i%2==1){ // 1 ---> 2 <---
int tmp = s;
s -= 2*(a-f);
if(s<0){
//printf("#%d %d %d\n")
s += (b-tmp);
if(s<0){
ok = 0;
break;
}
else {
count++;
}
}
}
else if(i%2==0){// 2 ---> 1 <---
int tmp = s;
s -= 2*f;
if(s<0){
s += (b-tmp);
if(s<0){
ok = 0;
break;
}
else {
count++;
}
}
}
// printf("%d %d\n",s,count);
}
if(k%2==1){ //1 --->
tmp = s;
s -= a-f;
if(s<0){
s += (b-tmp);
if(s<0){
ok = 0;
}
else count++;
}
}
else {
//1 <---
tmp = s;
s -= f;
if(s<0){
s += (b-tmp);
if(s<0){
ok = 0;
}
else count++;
}
}
if(ok==0){
printf("-1\n");
}
else {
printf("%d\n",count);
}
}
好的code:
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <cmath>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <ctype.h>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #define PI acos(-1.0)
- #define in freopen("in.txt", "r", stdin)
- #define out freopen("out.txt", "w", stdout)
- using namespace std;
- typedef long long ll;
- const int maxn = 200000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
- int aa, b, ff, k, ans;
- int a[maxn];
- int main() {
- scanf("%d %d %d %d", &aa, &b, &ff, &k);
- int n = k+1, f = 1;
- int t1 = 2*ff, t2 = 2*(aa-ff);
- a[1] = t1 / 2; f = 1-f;
- for(int i = 2; i < n; ++i) {
- if(f) {
- a[i] = t1;
- f = 1-f;
- }
- else {
- a[i] = t2;
- f = 1-f;
- }
- }
- if(f) a[n] = t1/2;
- else a[n] = t2/2;
- int c = b;
- for(int i = 1; i <= n; ++i) {
- //cout << a[i] << " == " << endl;
- if(c < a[i]) {
- c = b-a[i];
- ans++;
- }
- else {
- c -= a[i];
- }
- if(c < 0) { printf("-1\n"); return 0; }
- }
- printf("%d\n", ans);
- return 0;
- }