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

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;
}

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

时间: 2024-08-24 14:06:53

CodeForces - 864C-Bus-(模拟加油站问题)的相关文章

[Codeforces 864C]Bus

Description 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 point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 

codeforces 591B Rebranding (模拟)

Rebranding Problem Description The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding - an active marketing strategy, that includes a set of measures to change either the bra

Codeforces 389B(十字模拟)

Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Fox Ciel has a board with n rows and n columns. So, the board consists of n × n cells. Each cell contains either a symbol '.', or a s

CodeForces 697B Barnicle 模拟

强行模拟 纪念一下…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6 #include<string> 7 #include<map> 8 #include<vector> 9 #include<queue> 10 #define M(a

CodeForces - 344A Magnets (模拟题)

CodeForces - 344A Magnets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangula

CodeForces 670E(模拟双向链表)

题意:给你一串合法的括号和当前光标的位置和一些操作,问操作完之后的串是怎么样的 思路:模拟一个双向链表的操作,首先先预处理出配对的括号组,然后模拟即可 #include<bits\stdc++.h> using namespace std; const int maxn = 1e6; struct Node { int l,r; }nodes[maxn]; char s1[maxn],s2[maxn]; int a[maxn],d[maxn]; int main() { int n,m,pos

CodeForces 709B Checkpoints 模拟

题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stdio.h> 6 #include<stdlib.h> 7 #inc

Berland National Library codeforces 567B(模拟)

http://codeforces.com/problemset/problem/567/B 题意:图书馆有一个程序,可以记录学生的进出情况,'+'表示进入,'-'表示出去,字符后面的数字表示学生的编号.在这个程序尚未启动前,还有一些同学进的消息没有被记录下来.现在问你这个图书馆至少得容纳多少学生. 分析:用sum和ans两个值来记录,sum存当前房间人数,ans维护最大值. #include <iostream> #include <stdio.h> #include <s

CodeForces 825B(模拟&amp;贪心_D题)解题报告

题目链接:http://codeforces.com/problemset/problem/825/B -------------------------------------------------------------------------------- 题意:五子棋,在输入条件下,能否在当前局面获胜. 思路:很明显的是,当我们下五子棋时,我们每步都是进行一次搜索,观察能否连接成为5个.同理,利用计算机也可以向各个方向进行搜索.好在本题只是10X10的棋面,直接对每个点进行4个方向(水