Ants

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

Input

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

Output

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time. 

Sample Input

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output

4 8
38 207

百度翻译:一群蚂蚁在一根长l厘米的水平杆上行走,每根杆的速度恒定为1厘米/秒。当一只蚂蚁到达杆的末端时,
它立刻从杆上掉下来。当两只蚂蚁相遇时,它们会转身向相反的方向走。我们知道蚂蚁在柱子上的原始位置,不幸的是,
我们不知道蚂蚁行走的方向。你的任务是计算所有蚂蚁从极点上掉下来所需要的最早和最晚的可能时间。

思路:如果两只蚂蚁在相遇时交换身份,那么蚂蚁就可以一直沿一个方向走,题目就很简单了,要注意求的是所有的蚂蚁下落的时间,
一个蚂蚁落下的时间分为短时间和长时间,则最短时间应是所有蚂蚁落下的短时间的最大值,最长时间
是所有蚂蚁落下时间的长时间的最大值。


 1 #include <iostream>
 2 #include <cstdio>
 3 #include <fstream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <deque>
 7 #include <vector>
 8 #include <queue>
 9 #include <string>
10 #include <cstring>
11 #include <map>
12 #include <stack>
13 #include <set>
14 #include <sstream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int l,t,n;
22 int main()
23 {
24     cin>>t;
25     while(t--)
26     {
27         scanf("%d %d",&l,&n);
28         int s;
29         int mi=0;
30         int ma=0;
31         for(int i=0;i<n;i++)
32         {
33             scanf("%d",&s);
34             mi = max(mi,min(l-s,s));
35             ma = max(ma,max(l-s,s));
36 
37         }
38         printf("%d %d
",mi,ma);
39     }
40 }
原文地址:https://www.cnblogs.com/mzchuan/p/11173953.html