Ants

来源:互联网 发布:linux怎么改文件夹名字 编辑:程序博客网 时间:2024/05/16 06:14

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
题意:给你木板的长度和蚂蚁的个数,蚂蚁在木板上行走,(木板可以看成x轴),给你蚂蚁的坐标,它可以往左右两个方向走,当它和其他的蚂蚁相遇时,会反向走。求当所有蚂蚁都掉落的最少时间和最多时间
思路:
就一第一个例子为例,一号蚂蚁在2的位置,2号蚂蚁在6的位置,它们一个往左走一个往右走,在4相遇,随后两个转身,可是,你计算一下,1号蚂蚁走的距离不就是2号蚂蚁一直往左走,不转身的距离吗?因此可以看成每个蚂蚁都走自己的方向,即使相遇也不转身,这样在看代码就好理解了。
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int main(){int t,l,n,i,a;scanf("%d",&t);while(t--){int p=0,q=0,ans1=0,ans2=0;scanf("%d%d",&l,&n);for(i=0;i<n;i++){scanf("%d",&a);p=min(a,l-a);ans1=max(ans1,p);q=max(a,l-a);ans2=max(ans2,q);}printf("%d %d\n",ans1,ans2);}return 0;}

0 0
原创粉丝点击