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

参考别人的提示:可以将蚂蚁看成根本没有掉头,因为速度是一样的。这是解题的关键!

感觉被耍了吧?哈哈,脑筋急转弯啊!

下面贴代码。

01 #include<stdio.h>
02 #include<stdlib.h>
03 
   //Author: Runzhen Wang
04 void calculate(int len,int num)
05 {
06     int e_temp,l_temp,posit,i;
07     int earlist=-1,latest=-1,flag=len/2;
08     for(i=0;i<num;i++)
09     {
10         scanf(“%d”,&posit);
11         if(posit<=flag)
12         {
13             e_temp=posit;
14             //得到最快掉下去的时间 这里不能为 posit-1  !!!!
15             l_temp=lenposit;// 得到最慢掉下去的时间
16         }
17         else
18         {
19             e_temp=lenposit;//在右半边
20             l_temp=posit;//不能为 posit-1,否则POJ报错,不知为何
21         }
22         if(earlist<e_temp)
23         earlist=e_temp;
24         if(latest<l_temp)
25             latest=l_temp;
26     }
27         printf(“%d %d\n,earlist,latest);
28 
29 }
30 
31 int main()
32 {
33     int N,len,num;
34     scanf(“%d”,&N);
35     while(N)
36     {
37         scanf(“%d%d”,&len,&num);
38         calculate(len,num);
39     }
40 return 0;
41 }
Tags: ,. 5,618 views
Home

5 Comments so far

Trackbacks/Pingbacks

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
URLs must be fully qualified (eg: http://blog.nlogn.cn),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.