POJ 1852 Ants

Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14610   Accepted: 6363

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

 
 
 
解析:题意为n只蚂蚁以1cm/s的速度在长为Lcm的水平杆上爬行。当蚂蚁爬到杆的端点时会立即掉落。当两只蚂蚁相遇时,他们各自反向爬回去。对于每只蚂蚁,只知道它到杆左端的距离为xi,但不知道它的朝向。求所有蚂蚁从杆上落下的最短时间和最长时间。
1.最短时间很容易想到。所有蚂蚁都朝向距离自己较近的端点爬行,最后一只蚂蚁落下所花费的时间就是最短时间。事实上,这种情况下不会发生两只蚂蚁相遇的情况,而且也不可能比这有更短的时间走到杆的端点。
2.为了研究最长时间的情况,我们看看两只蚂蚁相遇会发生什么。根据题意,我们知道当两只蚂蚁相遇时,他们各自反向爬回去。假如无视蚂蚁之间的区别,从整体上看,我们可以认为两只蚂蚁各自保持原样前行。事实上,这样理解对于我们研究的问题不会有影响。因此,我们可以认为每只蚂蚁都是独立运动的,最长时间来源于到杆的某个端点的距离最远的那只蚂蚁。
 
 
 
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int len, n;
        scanf("%d%d", &len, &n);
        int minT = 0, maxT = 0;
        int pos;
        while(n--){
            scanf("%d", &pos);
            minT = max(minT, min(pos, len-pos));
            maxT = max(maxT, max(pos, len-pos));
        }
        printf("%d %d
", minT, maxT);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/inmoonlight/p/5691077.html