POJ 1852 Ants

Ants

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 1852
64-bit integer IO format: %lld      Java class name: Main
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 <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 const int INF = 0x3f3f3f3f;
 5 int main(){
 6     int n,m,theMin,theMax,T,cur;
 7     scanf("%d",&T);
 8     while(T--){
 9         scanf("%d %d",&n,&m);
10         theMin = theMax = 0;
11         for(int i = 0; i < m; ++i){
12             scanf("%d",&cur);
13             theMin = max(theMin,min(n-cur,cur));
14             theMax = max(theMax,max(n-cur,cur));
15         }
16         printf("%d %d\n",theMin,theMax);
17     }
18     return 0;
19 }
View Code

 

posted @ 2015-06-04 20:56  狂徒归来  阅读(233)  评论(0编辑  收藏  举报