Ants(POJ No.1852)

An army of ants walk on a horizontal pole of lengthlcm, each with a constant speed of 1 cm/s. Whena walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turnback 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 computethe earliest and the latest possible times needed for all ants to fall off the pole.
Input:
The rst line of input contains one integer giving the number of cases that follow. The data for eachcase start with two integer numbers: the length of the pole (in cm) andn, the number of ants residingon the pole. These two numbers are followed bynintegers giving the position of each ant on the poleas the distance measured from the left end of the pole, in no particular order. All input integers arenot bigger than 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The rst number is the earliestpossible 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
Output for sample input
4 8
38 207
题意描述:在不知道蚂蚁朝向的方向时,给出所有蚂蚁到左端点的距离,求出所有蚂蚁掉落的最小时间和最大时间。

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000001];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int l, n;
		int maxT = 0,minT = 0;
		scanf("%d%d", &l, &n);
		for (int i = 1; i <= n; i++)//将所有距离存入数组中
		{
			scanf("%d", &a[i]);
		}
		for (int i = 1; i <= n; i++)//当所有的蚂蚁都不会碰面的时候,蚂蚁向距离两端最近的一端爬行,最后一只蚂蚁掉落的时间就是所有蚂蚁掉落的最小值
		{
			minT = max(min(a[i], l - a[i]), minT);//找出所有最短用时中的最大值就是所有蚂蚁掉落的最小时间
		}
		for (int i = 1; i <= n; i++)//因为两只蚂蚁碰面的时候所用的时间是相同的,如果此时调头就相当于蚂蚁1走了蚂蚁2剩下的路,蚂蚁2走了蚂蚁1剩下的路
			                        //所以,可以看做两只蚂蚁碰面后继续直走不掉头,所用的时间是不变的。
		{
			maxT = max(max(a[i], l - a[i]), maxT);//找出所有最长用时中的最大值就是所有蚂蚁掉落的最大时间
		}
		printf("%d %d\n", minT, maxT);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值