POJ1852-Ants

来源:互联网 发布:人工智能 下半场 编辑:程序博客网 时间:2024/05/17 10:26

Ants
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 18143 Accepted: 7680

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

210 32 6 7214 711 12 7 13 176 23 191

Sample Output

4 838 207

Source

Waterloo local 2004.09.19

题意:n只蚂蚁都在长度为L(cm)的膀子上爬行,它们的速度都是1cm/s,到了棒子终端的时候,蚂蚁就会掉下去。如果在爬行途中遇到其他蚂蚁,两只蚂蚁的方向都会逆转。已知蚂蚁在棒子的最初位置坐标,但是我们不知道他们会往哪一个方向爬。请求出所有蚂蚁掉下去的最短时间和最长时间

解题思路:最短时间就是所有蚂蚁距离两端点距离较小的距离中取大者就是所需最短时间,而最长时间就是所有蚂蚁距离两端点距离较大者中取大者就是所需最长时间


#include <iostream>  #include <cstdio>  #include <cstring>  #include <string>  #include <algorithm>  #include <cmath>  #include <map>  #include <cmath>  #include <set>  #include <stack>  #include <queue>  #include <vector>  #include <bitset>  #include <functional>  using namespace std;#define LL long long  const int INF = 0x3f3f3f3f;int a[1000009], n, l;int main(){int t;scanf("%d", &t);while (t--){scanf("%d%d", &l, &n);int mi=-1, ma=-1;for (int i = 1; i <= n; i++){scanf("%d", &a[i]);mi = max(mi, min(a[i], l - a[i]));ma = max(ma, max(a[i], l - a[i]));}printf("%d %d\n", mi, ma);}return 0;}

原创粉丝点击