题目地址
http://codeforces.com/contest/864/problem/C
题目大意
总路程a,油箱容量b,加油站离出发点f,总共要跑k趟,问,至少需要加油多少次
分析
以每次到达加油站为基准点,判断是否可以跑完下一个端点–加油站的行程,如果可以,不加油,不可以,加油
坑
有几种特殊情况
1.沿着当前方向,跑到端点结束,没有再从端点回加油站的过程
2.根本跑不到加油站
3.沿着当前方向,加一次油跑到下一个端点,结束
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import java.util.*; /** * Created by u3 on 16-10-16. */ public class Bus{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int f = scanner.nextInt(); int k = scanner.nextInt(); int current;//现在的油量 int j = 0;//行程数量 int num = 0; current = b - f;//减去第一次跑到加油站的油 while (j < k) { if (current < 0){ j = -1; break; } if (current - 2 * (a - f) >= 0) {//判断到加油站时,能不能到左边再返回不用加油 current -= 2 * (a - f); } else if (current - (a - f) >= 0 && j + 1 == k) {//判断是不是最后一趟,可以直接跑到 j++; continue; }else if (b - (a - f) >= 0 && j + 1 == k) {//判断是不是最后一趟,可以加油跑到 j++; num++; continue; }else {//要加油 current = b - 2 * (a - f); if (current < 0) { j = -1; break; } num++; } j++; if (j == k) break; if (current >= 2 * f) {//判断到加油站时,能不能到右边再返回不用加油 current -= 2 * f; } else if (current - f >= 0 && j + 1 == k) {//判断是不是最后一趟,可以直接跑到 j++; break; } else if(b-f>=0&&j+1==k){//判断是不是最后一趟,可以加油跑到 num++; j++; break; }else{//要加油 current = b - 2 * f; if (current < 0) { j = -1; break; } num++; } j++; if (j == k) break; } if (j == -1) { System.out.println(j); } else { System.out.println(num); } } } |
[……]