您的位置:首页 > 其它

2376_ants(巧妙避开了申请大容量数组,思路上采用单个蚂蚁独立分析,而非将全部数据输入后才统一比较)

2014-06-16 22:26 423 查看
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,某只蚂蚁位置X,由于相遇不影响结果,求最长时间只需考虑那只可

以离边界最大值最大的蚂蚁,求最短时间只需考虑那只离边界最小值最大的蚂蚁,所以

最长时间为Max(n只蚂蚁的Max(X,L-X)),最短时间为Max(n只蚂蚁的Min(X,L-X))。

*/
//转载

#include<iostream>
using namespace std;
int main()
{
int l,n;
int t;
int min,max;
int r,s;
cin >> l;
while(cin >> l >> n)
{
min = -1;
max = -1;
for(int i = 0;i < n;i++)
{
cin >> t;
r = l - t;
if(r > t)
s = t,t = r,r = s;//如此操作后:r为此蚂蚁到两端的最短距离,t为最长距离

if(min < r)//求n个蚂蚁r的最大值
min = r;
if(max < t)//求n个蚂蚁t的最大值
max = t;
}
cout << min << " " << max << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐