码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 864C-Bus-(模拟加油站问题)

时间:2019-04-18 22:11:03      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:algorithm   get   不能   src   inf   can   if语句   cstring   stdio.h   

https://vjudge.net/problem/CodeForces-864C

题意:两地之间有个加油站,往返走k个单程,最少加油多少次。

大佬几十行代码就解决,我却要用一百多行的if语句模拟解决。

技术图片
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

ll a,b,f,k;
ll one,two;///加油站左边路程为one,右边路程为two
ll t;
ll nowb;
ll sum;

int main()
{
    while(scanf("%lld %lld %lld %lld",&a,&b,&f,&k)!=EOF)
    {
        one=f;
        two=a-f;
        nowb=b;            ///当前体力
        sum=a*k;           ///总路程
        t=0;               ///补充体力次数
        ll i;              ///当前走了多少路程
        bool flag=true;   ///能否走到
        int now=1;        ///now表示方向,1为右,-1为左

        for(i=0;i<sum;)///不加等于,不要i++
        {
            if(i==0)///刚出发
            {
                if(nowb>=one)///能走到加油站
                {
                    i+=one;
                    nowb-=one;
                }
                else///否则直接凉
                {
                    flag=false;
                    break;
                }
            }
            else if( k%2 && i==sum-two)///奇数次,最后向右,最后要走two这一段
            {
                if(nowb>=two)///走得到
                {
                    i+=two;
                    nowb-=two;
                }
                else///走不到,加完再看能不能到
                {
                    nowb=b;
                    t++;
                    if(nowb>=two)
                    {
                        i+=two;
                        nowb-=two;
                    }
                    else
                    {
                        flag=false;
                        break;
                    }
                }
            }
            else if( k%2==0 && i==sum-one)///偶数次,最后向左,最后要走one这一段
            {
                if(nowb>=one)
                {
                    i+=one;
                    nowb-=one;
                }
                else
                {
                    nowb=b;
                    t++;
                    if(nowb>=one)
                    {
                        i+=one;
                        nowb-=one;
                    }
                    else
                    {
                        flag=false;
                        break;
                    }
                }
            }
            else ///中间跑路,走两段one或者two
            {
                if(now==1)///往右跑
                {
                    if(nowb>=2*two)///跑得到,减油,改方向
                    {
                        i+=2*two;
                        nowb-=2*two;
                        now=-1;
                    }
                    else ///否则,加油
                    {
                        t++;
                        nowb=b;
                        if(nowb>=2*two)///加完看能不能跑到
                        {
                            i+=2*two;
                            nowb-=2*two;
                            now=-1;
                        }
                        else
                        {
                            flag=false;
                            break;
                        }
                    }
                }
                else ///往左跑
                {
                    if(nowb>=2*one)
                    {
                        i+=2*one;
                        nowb-=2*one;
                        now=1;
                    }
                    else ///否则,加油
                    {
                        t++;
                        nowb=b;
                        if(nowb>=2*one)///加完看能不能跑到
                        {
                            i+=2*one;
                            nowb-=2*one;
                            now=1;
                        }
                        else
                        {
                            flag=false;
                            break;
                        }
                    }
                }

            }
        }
        if(flag)
            printf("%d\n",t);
        else
            printf("-1\n");
    }
    return 0;
}
View Code

 

CodeForces - 864C-Bus-(模拟加油站问题)

标签:algorithm   get   不能   src   inf   can   if语句   cstring   stdio.h   

原文地址:https://www.cnblogs.com/shoulinniao/p/10732498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!