POJ4852 Ants

Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20047   Accepted: 8330

Description

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

Source

 
【题解】
蚂蚁可以传过去。于是最大值就是每只蚂蚁向远的那一段走,最小值
就是每只蚂蚁向近的那一段走。每只蚂蚁的值取最大
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <algorithm>
 6 #define max(a, b) ((a) > (b) ? (a) : (b))
 7 #define min(a, b) ((a) < (b) ? (a) : (b))
 8 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
 9 
10 const int MAXN = 3000000 + 10;
11 const int INF = 0x3f3f3f3f;
12 
13 inline void read(int &x)
14 {
15     x = 0;char ch = getchar(), c = ch;
16     while(ch < '0' || ch > '9')c = ch, ch = getchar();
17     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
18 } 
19 
20 int t,n,l,num,ma,mi;
21 
22 int main()
23 {
24     read(t);
25     for(;t;--t)
26     {
27         read(l);read(n);
28         mi = ma = 0;
29         for(register int i = 1;i <= n;++ i) 
30         {
31             read(num);
32             mi = max(mi, min(num, l - num));
33             ma = max(ma, max(num, l - num));
34         }
35         printf("%d %d
", mi, ma);
36     }
37     return 0;
38 } 
POJ4852
 
 
原文地址:https://www.cnblogs.com/huibixiaoxing/p/7648457.html