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) andn, the number of ants residing on the pole. These two numbers are followed byn 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.
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.
2 10 3 2 6 7 214 7 11 12 7 13 176 23 191
4 8 38 207 按着题意做相撞则转向,对于最少时间很容易想到就是一次都没有碰撞,n/2,左边的向左,右边的向右; 但是对于最长时间,似乎是要碰撞次数尽量多但是还与碰撞位置有关,毫无头绪,原来可以等价转换Y——Y,A,B碰撞反向运动可以等价为A,B保持原方向运动,想了毛毛久原来这么简单......#include<stdio.h> #include<math.h> int main() { int n,m,t,min,max,x; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); min=0; max=0; while (m--) { scanf("%d",&x); if (abs(min-n/2)>abs(x-n/2)) min=x; if (n-x>max) max=n-x; if (x>max) max=x; } if (n-min<min) min=n-min; printf("%d %d\n",min,max); } return 0; }