Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 258 Accepted Submission(s): 114
Problem Description
XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each pillar has a jewel.Now XY is standing on the S-th pillar and the exit is in the T-th pillar.XY can leave from the exit only after they get all the jewels.Each time XY can move to adjacent pillar,or he can jump to boundary ( the first pillar or the N-th pillar) by using his superpower.However,he needs to follow a rule:if he left the pillar,he no can not get here anymore.In order to save his power,XY wants to use the minimum number of superpower to pass the game.
Input
There are multiple test cases, no more than 1000 cases.
For each case,the line contains three integers:N,S and T.
(1≤N≤10000,1≤S,T≤N)
Output
The output of each case will be a single integer on a line: the minimum number of using superpower or output -1 if he can't leave.
Sample Input
Sample Output
Source
BestCoder Round #61 (div.2)
|
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n,st,ed;
int ans;
int main()
{
while(~scanf("%d%d%d",&n,&st,&ed))
{
if(n==1)ans=0;//只有一根柱子
else if(st==ed)ans=-1;//无法再返回了
else
{
bool ST,ED,MD;
ans=0;
if(st<ed)
{
ST=st!=1;
ED=ed!=n;
MD=ed-st>1;
}
else//if(st>ed)
{
ST=st!=n;
ED=ed!=1;
MD=st-ed>1;
}
if(ST)ans=ST+MD;
else ans=ED;
}
printf("%d\n",ans);
}
return 0;
}
/*
【题意】
有n(1e4)个柱子,从1到n排成一排。
我们每个柱子必须抵达一次且只能抵达一次。
一开始我们在st点,我们希望最后一次抵达的点是ed点。
我们随时都可以传送,传送到1点或n点。
问你至少的传送次数。
【类型】
模拟
【分析】
首先很好确定,答案必然是-1,0,1,2四者之一。
因为一旦传送到1或n之后再也不能到达1或n了。
所以问题就是,什么时候为-1或0或1或2。
方法一:暴力做法,就是看这2个点所划分出的3个区间的有无,枚举所有8种可能得到答案。
方法二:针对4种答案分开讨论。
用ST==1表示st那侧有点或没有点
用MD==1表示中间有点或没有点
用ED==1表示ed那侧有点或没有点
-1:n>1且st==ed
0:!ST&&!ED
1:!ST||!MD
2:else
方法三:起点式思维
if(ST)ans=ST+MD;
else ans=ED
【时间复杂度&&优化】
O(1)
【数据】
以下是覆盖一切情况的数据
1 1 1
ans=0
2 1 1
ans=-1
情况0 0 0
2 1 2
ans=0
情况0 0 1
3 1 3
ans=0
情况0 1 0
3 1 2
ans=1
情况0 1 1
4 1 3
ans=1
情况1 0 0
3 2 3
ans=1
情况1 0 1
4 2 4
ans=2
情况1 1 0
4 2 3
ans=1
情况1 1 1
5 2 4
ans=2
*/