poj 1852 Ants <规律题>

来源:互联网 发布:java中图片上传和回显 编辑:程序博客网 时间:2024/04/30 07:14
Ants

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 12128

 

Accepted: 5322

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
 
思路:
 
          当这条路上就两只蚂蚁的时候,有两种情况:1.不用相碰,直接掉下棒;2.相碰后反向移动,然后掉下棒(在这种情况中,由于碰前所走的路与在碰后同样要走,然后返回到原位,然后继续走,相当于与它相碰的对方(与它相向走的蚂蚁))所走的距离,又因为蚂蚁都是完全一样的,他们不管是谁走的都一样,最终所得到的所有的长度的个数和种类都一样,所以就直接算两个方向的距离来求最大值最小值就行了!然后在较小的距离中找最大值,在较大的距离中找最大值,输出就行了!(多只蚂蚁和两只蚂蚁的做法相同,相碰之后他们的值只发生交换,但是最终所有的值并不发生给变!)
 
代码:
 
 
#include <stdio.h>#include <string.h>#define max(a,b) (a>b?a:b)#define min(a,b) (a>b?b:a)#define INF 0x3f3f3f3fint main(){int T;scanf("%d",&T);while(T--){int n,m,a,max=0,min=0;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){scanf("%d",&a);max=max(max,max(a,n-a));//在每只蚂蚁中的两种可能的情况下,找最大值,然后在所有的最大值中找最大值,这样才能保证所有的蚂蚁都能掉下棒 min=max(min,min(a,n-a));//最小的也一样,就是在所有的两种情况中找最小值,然后在所有的最小值中找最大值! }printf("%d %d\n",min,max);}return 0;}

0 0
原创粉丝点击