POJ 1852 思维

http://poj.org/problem?id=1852

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/s的速度在杆子上爬行,当蚂蚁爬到杆子的端点时会掉落,当两只蚂蚁相遇时,它们不能交错通过智能各自反向爬回去,给出每只蚂蚁距离左侧端点的距离,每只蚂蚁的朝向是未知的,求所有蚂蚁落下杆子的最短时间和最长时间。

思路:这是一道考察思维的题目,按照题意,两只蚂蚁相遇后完全可以当作是方向不变、交错通过。因为蚂蚁的朝向是我们自己决定的,蚂蚁都朝左或都朝右两种情况下的最大值和最小值就是我们要求的答案。

#include<iostream>
#include<cstdio>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;

int MAX,MIN,L;

int read()
{
    int temp=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
        ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        temp=temp*10+ch-48;
        ch=getchar();
    }
    return temp;
}

int main()
{
    int  t=read();
    while(t--)
    {
        int n,temp;
        MAX=MIN=0;
        L=read(),n=read();
        for(int i=0;i<n;i++)
        {
            temp=read();
            MAX=max(MAX,max(temp,L-temp));
            MIN=max(MIN,min(temp,L-temp));
        }
        printf("%d %d\n",MIN,MAX);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88773951