poj1852——Ants(模拟)

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只蚂蚁距左端的距离,蚂蚁行走的速度正好是单位速度,求至少和至多需要多久,最后一个蚂蚁到达端点,两只蚂蚁若相撞则方向都相反。
其实相撞了可以看作没相撞,因为速度是不变的,所以要先把蚂蚁走两边各需要多久算出来,最少时间是所有每个蚂蚁的最短时间的最大值,最长时间就是每一个的最长时间

#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define MAXN 1000005
#define Mod 10001
using namespace std;
int left1[MAXN],right1[MAXN],m[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int l,n,maxnum=-1,minnum=-1;
        scanf("%d%d",&l,&n);
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&left1[i]);
            right1[i]=l-left1[i];
            maxnum=max(maxnum,max(left1[i],right1[i]));
            m[i]=min(left1[i],right1[i]);
            minnum=max(minnum,m[i]);
        }
        printf("%d %d\n",minnum,maxnum);
    }
    return 0;
}

你可能感兴趣的:(模拟,POJ题目小练)