POJ 1852 Ants

 Ants
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 9659
Accepted: 4268

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

Waterloo local 2004.09.19


题目链接  :http://poj.org/problem?id=1852


题目大意  :有一根横杆长len,上面有n只蚂蚁以一个单位长度每秒前进,蚂蚁的初始位置知道(横杆从左往右方向上的坐标),初始的方向不知道,两只蚂蚁相遇后则反向,蚂蚁走到横杆端点后掉落,求所有蚂蚁掉落的最短和最长时间


题目分析  :一道有意思的水题,我们可以将两只蚂蚁相遇后反向的过程当成相遇后继续前进
例如: 两只蚂蚁相遇 -> <- 反向 <- -> 反向的过程可以等价为两只蚂蚁分别又往前走了一步,因此只需要考虑每只蚂蚁向左走和向右走的步数,它们之中最小值的最大值即为最短时间,最大值的最大值即为最长时间


#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int T, len, num, pos;
    int most,least;
    int mpos, lpos;
    scanf("%d",&T);
    while(T--)
    {
        most = 0;
        least = 0;
        scanf("%d %d",&len, &num);
        while(num--)
        {
            scanf("%d", &pos);
            mpos = max(pos,len - pos);
            lpos = min(pos,len - pos);
            if(lpos > least)
                least = lpos;
            if(mpos > most)
                most = mpos;
        }
        printf("%d %d\n",least, most);
    }
}



你可能感兴趣的:(poj,水题)