题目链接:
http://poj.org/problem?id=1852
题目意思:
有n只蚂蚁,在长为l的杆子上,开始方向未知,各自沿着初始方向以单位速度爬,当两蚂蚁相遇时两只蚂蚁都向相反的方向爬,求所有蚂蚁都离开杆子的最短时间和最长时间。
解题思路:
思维转换,当两只蚂蚁相遇都掉头时,可以等价处理为蚂蚁不改变方向交错的继续往前爬。问题就等价为max(蚂蚁到两段的距离最大或最小)问题喽。
代码:
//#include<CSpreadSheet.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,l,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&l,&n);
int Min=0,Max=0;
for(int i=1;i<=n;i++)
{
int cur;
scanf("%d",&cur);
if(l-cur>cur)
{
Min=max(Min,cur);
Max=max(Max,l-cur);
}
else
{
Min=max(Min,l-cur);
Max=max(Max,cur);
}
}
printf("%d %d\n",Min,Max);
}
return 0;
}