zoukankan      html  css  js  c++  java
  • Codeforces Gym 100637B B. Lunch 找规律

    B. Lunch

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/gym/100637/problem/B

    Description

    The swamp looks like a narrow lane with length n covered by floating leaves sized 1, numbered from 1 to n with a fly sitting on the top of each. A little toad is sitting on one of the leaves instead of a fly. Its name is Kvait and it is about to have lunch. It can jump to the bordering leaf or jump it over to the next one in any direction. When landing it eats a fly. Kvait is already quite a big toad and the leaves are unstable so when it jumps away the leaf starts sinking.

    In order to have lunch Kvait needs to eat all of the flies. It starts his journey from the leaf with number s and has to finish on the leaf with number f. Yet jumping to the bordering leaf takes more Kvait’s energy than skipping a leaf over. It is necessary to plan the toad’s movements to get lunch with minimal energy spent.

    Input

    Single line contains three integers nsf (2 ≤ n ≤ 10 000, 1 ≤ s, f ≤ n) — the number of leaves, number of a starting leaf and the number of the finish leaf respectively.

    Output

    Output the minimal number of jumps to the bordering leaves required for the toad to have lunch. If there is no way to eat up, output a single number  - 1.

    Sample Input

    4 1 2

    Sample Output

    1

    HINT

    题意

    有n个点,每次青蛙可以跳一步或者两步,要求从s点遍历全部点并且最后落在f点,要求最少跳一步的步数是多少

    题解:

    我是写DFS,然后暴力对拍调的

    我们跳3步的话,可以只需要挑一步,其他时候都需要跳完……

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    const int maxn=202501;
    #define mod 1000000007
    #define eps 1e-9
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //*************************************************************************************
    
    
    int main()
    {
        int n=read(),f=read(),s=read();
        int ans=0;
        if(f<s)
            swap(f,s);
        if(s==f)
        {
            cout<<"-1"<<endl;
            return 0;
        }
        if(s!=1)
        {
            ans+=1;
            s=s+1;
            if(s==f)
            {
                if(f==n)
                    cout<<ans<<endl;
                else
                    cout<<"-1"<<endl;
                return 0;
            }
        }
        if(f!=n)
        {
            ans+=1;
            f-=1;
            if(s==f)
            {
                cout<<ans<<endl;
                return 0;
            }
        }
        ans+=((f-s)/3)+(f-s)%3;
        cout<<ans<<endl;
    }
  • 相关阅读:
    TIOBE 11月指数:C语言居首,稳居宝座,Python直逼第二!
    毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
    用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!
    趣文分享:C 语言和 C++、C# 的区别在什么地方?
    C++基础知识篇:C++ 基本语法
    盘点那些争议最大的编程观点,你是什么看法呢?
    小米死磕硬核技术,将扩招5000名工程师,多个领域会使用到C++
    Gentle guide on how YOLO Object Localization works with Keras (Part 2)
    HUMBLE YOLO IMPLEMENTATION IN KERAS
    Training and Detecting Objects with YOLO3
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4674268.html
Copyright © 2011-2022 走看看