POJ-1852

Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 19297   Accepted: 8059

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

 

题意:

一根细杆上有n只蚂蚁,我们只知道它们的位置却不知道它们的朝向。它们以1cm的速度爬行,越过杆的端点就会掉落,两两相遇就会掉头。

求全部掉落的最短时间和最长时间。

 

最短时间就是全部像离自己最近的端点移动,由于蚂蚁的速度都是一样的,不会发生碰撞。

对于最大的情况,我们可以将两只蚂蚁相遇后掉头看作交叉过去继续走,由此我们也只需要让所有蚂蚁都向最远的端点移动,即可求出最大值。

 

AC代码:

 1 #include<bits/stdc++.h>//注意,POJ是不能直接使用这个头文件的 提交时记得换为<iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     ios::sync_with_stdio(false);
 6     int t,n,l,x;
 7     cin>>t;
 8     while(t--){
 9         int MIN=0,MAX=0;
10         cin>>l>>n;
11         for(int i=0;i<n;i++){
12             cin>>x;
13             MIN=max(MIN,min(x,l-x));
14             MAX=max(MAX,max(x,l-x));
15         }
16         cout<<MIN<<" "<<MAX<<endl;
17     }
18     return 0;
19 } 

 

posted @ 2017-07-26 15:00  Kiven#5197  阅读(209)  评论(0编辑  收藏  举报