zoukankan      html  css  js  c++  java
  • 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 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 tox = 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 pointx = 0.

    input
    6 10 2 4
    output
    2
    input
    6 5 4 3
    output
    -1
    Note

    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 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

    一句话题意:***好像又不能一句话了。给你四个整数a,b,f,k,

    a表示你要开车从0到a,b表示你的油桶的容量为b,f表示可以在坐标为f的位置加油(f<a),k表示你要走k趟(0到a和a到0算两趟)

    问完成k次最少加几次油,一开始油桶满

    其实这道题并没什么思维含量,顶多是比较多的坑点吧,但是又因为pp的数据超强,导致并没有很多人fst

    总的说就是暴力枚举状态,我是枚举汽车到0或a时的状态,根据当前油量,判断是否要在此次途中加油

    注意:(1)如果第一次都到达不到加油点,输出-1

    (2)如果加过了都大不了对面,输出-1

    (3)在处理0,a的时候要千万小心

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int a,b,k,f;
     4 void print() {puts("-1"); exit(0);}
     5 int main(){
     6     scanf("%d%d%d%d",&a,&b,&f,&k);
     7     int res=b,i=0,pos=0,ans=0;
     8     while (i<k-1){
     9         if (res<abs(pos-f)) print; i++; 
    10         if (res<(a*2-abs(pos-f))){
    11             ans++; if (i%2==1) res=b-(a-f); else res=b-f;
    12         } else res-=a;
    13         if (res<0) print();
    14         if (i%2==1) pos=a; else pos=0;
    15     }
    16     if (res<abs(pos-f)) print(); if (pos==0) pos=a; else pos=0;
    17     if (b<abs(pos-f)) print(); if (res<a) ans++;
    18     printf("%d
    ",ans);
    19 }
    View Code
  • 相关阅读:
    学编程的那些年
    iOS React Native 学习总结
    java.util.ConcurrentModificationException异常原因及解决方法
    把二元查找树转变成排序的双向链表
    五猴分桃通解公式-敬献给诺贝尔奖获得者李政道博士
    JSP中文乱码问题的由来以及解决方法
    MyEclipse 2014 有用的几个快捷键
    jsp三层架构
    数据库连接的三层架构
    helloworld
  • 原文地址:https://www.cnblogs.com/logic-yzf/p/7612726.html
Copyright © 2011-2022 走看看