浅析 POJ 1852 Ants

Ants

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 n 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


Source

Waterloo local 2004.09.19

 

 

题目的大意为:

一群蚂蚁以1cm/s的速度在水平的杆子上行走,当蚂蚁走到杆子的端点时,将会从杆子上掉下。当两只蚂蚁在杆子上相遇时,会各自反转方向,继续行走。目前,已知所有蚂蚁在杆子上的原始位置,却不知道它们原始的行走方向。计算任务为,计算蚂蚁全部从杆上掉下的最短和最长时间。

 

输入:

第一行:表示样例数目

第二行:杆子长度和蚂蚁数量

第三行:依次为蚂蚁距离杆子左端点的距离

所有的输入值为不大于1000000的整数,且以空格符间隔

 

输出:

每一个测试样例,输出两个值:

第一个值是最短时间,第二个值是最长时间

 

问题分析:

首先分析问题,如果直接对多个蚂蚁,进行分析,在加上行走方向未知,可以说直接大脑就错乱了-----很多蚂蚁,方向未定,穷举搜呗。当n稍微大些,程序必然不奏效。

 

从较少数量的蚂蚁开始分析,试图发现规律。

 

最短情况:

仅有一只:

 

 

 

很明显,向距离较近的端点方向走,时间短

 

当有很多蚂蚁时:

 

 


规律同样适用,每个蚂蚁向最近的端点走,不会发生碰撞,且时间最短。

 

时间最长:

时间最长较时间最短,比较难思考。

一个蚂蚁时:

 

 

 

 

向较远的方向走,

当有多个蚂蚁时,则需要考虑蚂蚁的碰撞问题了。

蚂蚁碰撞:

 

 浅析 POJ 1852 Ants_第1张图片




  

所以,问题就变成求所有蚂蚁中,离端点最远的距离。

 


至此,所有问题,变成简单的遍历搜索,算法时间复杂度为(n

 

C++代码:


/*
* this code is to solve the poj 1852 ants problem
* date: 2015-05-11
* version: 0.1
* @author: zhang
* compiled by g++
*/

#include <cstdio>

#define Max(a,b) (a>b) ? a:b
#define Min(a,b) (a<b) ? a:b

#define MAXN 1000000

using namespace std;

int main()
{

	int ants[MAXN];
	int num_case;
        
	scanf("%d",&num_case);

	while(num_case--)
	{
		int polelen;
		int num_ants;
		scanf("%d %d",&polelen,&num_ants);

		for(int i=0;i<num_ants;i++)
		{
			scanf("%d",&ants[i]);
		}

		//the min time(length)

		int minL;
		minL=0;
		for(int i=0;i<num_ants;i++)
		{
			int tmpmin;
			tmpmin=0;
			tmpmin=Min(ants[i],polelen-ants[i]);
			minL=Max(minL,tmpmin);
		}


		//the max time(length)

		int maxL;
		maxL=0;
		for(int i=0;i<num_ants;i++)
		{	
			int tmpmax;
			tmpmax=0;
			tmpmax=Max(ants[i],polelen-ants[i]);
			maxL=Max(maxL,tmpmax);
		}
		printf("%d %d\n",minL,maxL);
	}
	return 0;
}



 

你可能感兴趣的:(算法,poj)