Ants
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26524 | Accepted: 10379 |
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的速度在长为Lcm的竿子上爬行。当蚂蚁爬到竿子的端点时就会掉落。由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反向爬过去。对于每只蚂蚁,我们知道它距离竿子左端的距离Xi,但不知道它当前的朝向。清计算所有蚂蚁落下竿子所需的最短时间和最长时间。
题解
前提蚂蚁的方向不确定。
考虑第一种情况,时间最短,当蚂蚁向一个方向走(且方向是蚂蚁当前位置到杆的两端距离最短的那个方向)直至掉下去,用时最短,所以所有蚂蚁分别掉下去所用最小的时间的最大值为所求的最小时间。
考虑第二种情况,时间最长,当两只蚂蚁碰头后,反向走,可以认为是,两只蚂蚁没有碰头,继续沿着原来的方向走,直至掉落,这是等效的,所以蚂蚁向一个方向(且方向是蚂蚁当前位置到杆的两端距离最大的那个方向)直至掉下去,用时最长,所以所有蚂蚁分别掉下去所有最大的时间的最大值为所求的最大时间。
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX_N = 100001;
int main()
{
int N;
scanf("%d", &N);
while(N--){
int poleLen, n;//竿子长度和n只蚂蚁
int pole[MAX_N];//竿子刻度
int i;
while(scanf("%d %d", &poleLen, &n) != EOF){
for(i = 0; i < n; i++){
scanf("%d", &pole[i]);//蚂蚁距离竿子左端的距离
}
int poleMin = 0;
for(i = 0; i < n; i++){
poleMin = max(poleMin, min(pole[i], poleLen - pole[i]));//最短时间
}
int poleMax = 0;
for(i = 0; i < n; i++){
poleMax = max(poleMax, max(pole[i], poleLen - pole[i]));//最长时间
}
printf("%d %d\n", poleMin, poleMax);
}
}
return 0;
}