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

题意:长m的杆,有n个蚂蚁,知道每个蚂蚁的位置,每个蚂蚁1s走1单位长度,方向不知道,两个蚂蚁撞到就会反向走。问所有蚂蚁到达两端的最长和最短时间
分析:很妙的一点:我们不需要知道单个蚂蚁具体怎么走的,其实就相当于两个蚂蚁对着走,直接穿过去,不用转头。比如蚂蚁a和蚂蚁b相遇,
   我们不让他们转头,相遇后b走的其实就是a应该转头走的。
 1 /*************************************************************************
 2     > File Name: r.cpp
 3     > Author: LiuGeXian
 4     > Mail: [email protected] 
 5     > Created Time: 2020/4/14 18:31:08
 6  ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cmath>
10 #include <iostream>
11 using namespace std;
12 int T, n, m;
13 int main(){
14     scanf("%d" ,&T);
15     while (T--){
16         int minn = -1, maxn = -1;
17         scanf("%d%d", &m, &n);
18         for (int i = 1; i <= n; i++){
19             int a;
20             scanf("%d", &a);
21             minn = max(minn, min(a, m - a));
22             maxn = max(maxn, max(a, m - a));
23         }
24         printf("%d %d\n", minn, maxn);
25     }
26     return 0;
27 }
View Code

猜你喜欢

转载自www.cnblogs.com/ghosh/p/12701740.html