# UVa OJ 10714 – Ants

## Solution

### Thinking

If all the ants are going to the nearest end of the pole, it is easy to find the earliest time needed.

#### Find the latest time （找出最长时间）

If there are two or more ants, pole can be likened to a relay race field, and the ants are like passing the baton. When the ants meet, they replace each other and walk the rest of the way to the end of the pole. The transfer of the baton here can be understood as the consumption of time. When the end of the pole is reached, the relay ends and the time expires.

Further, we can think that the ants will continue to walk forward when meeting each other , which is equivalent to the ants turning back.. So if you want to find the latest time, just find the maximum distance from the ant to the end of the pole.

### C++ code

```#include<cstdio>
using namespace std;
int main(void){
int n;
scanf("%d",&n);
while(n--){
int len,ants,min=0,max=0;
scanf("%d %d",&len,&ants);
while(ants--){
int ant,min_t,max_t;
scanf("%d",&ant);
if(ant< len-ant){
min_t=ant;
max_t=len-ant;
}else{
min_t=len-ant;
max_t=ant;
}
if(min<min_t)
min=min_t;
if(max<max_t)
max=max_t;
}
printf("%d %d\n",min,max);
}
return 0;
}
```