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

 

你可能感兴趣的:(codeforces)