Ants POJ 1852

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

//大意就是找出最大最小的时间,蚂蚁都掉下去了

//可以这样想,当两只蚂蚁相碰时,不是都要向回走吗?可以理解成两只蚂蚁是一样的,就相当于两只蚂蚁互不影响,即使相碰他还是接着先前走。

AC代码
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define M 1000000
int min[M], max[M];
int main()
{
	int len, N, temp;
	int left, right;
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d", &len, &N);
		for(int i = 1; i <= N; ++i){
			scanf("%d",&temp);
			left = temp;
			right = len-temp;
			if(left>right){
				min[i] = right;
				max[i] = left;
			}
			else
			{
				min[i] = left;
				max[i] = right;
			}
		}
		int Min=-INF, Max=-INF;
		for(int i = 1; i <= N; ++i){
			if(min[i] > Min) Min = min[i];
			if(max[i] > Max) Max = max[i];
		}
		printf("%d %d\n", Min, Max);
	}

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-08 20:39:18

Ants POJ 1852的相关文章

加强赛(二)E - Ants POJ - 1852

感谢我的学长对我的带领:该题思想来源于WArobot: 输入数据: N(N组测试数据) L (绳长)n(蚂蚁的个数) 接下来是n个数据(n个蚂蚁每个在绳子上距离绳子最左端的距离l) 该题给出每个蚂蚁的速度均为1cm/s,所以该问题无需考虑: 输出: 蚂蚁的最短掉落时间和最长的掉落时间: 掉落的最短时间: 找出每一只蚂蚁离最左端的距离和离最右端的距离的最小值再将这些数据比较,找出最大的,因为掉落的最短时间取决于最后一个掉落的蚂蚁的时间: 掉落的最长时间: 其实条件中的两只蚂蚁相遇往回反的条件可以忽

Ants POJ - 1852 弹性碰撞+模拟

题目 https://vjudge.net/problem/POJ-1852 题意:在一个固定长度的木条上面有n只蚂蚁,每个蚂蚁的速度一样,方向任意(可由自己决定初始方向),每只蚂蚁碰头后会朝相反反向前进,问所有蚂蚁都从木条上掉下去(走到左右端点处)的最短和最长时间是多少? 这篇博客讲的挺好 https://www.cnblogs.com/zhenghao2/p/6446065.html 思路: 1.这道题一开始思考最大时间的时候,可能会想,如果在木条上面一直碰头变换位置,那么时间一定会是最大的

poj 1852&amp;3684 题解

poj 1852 3684 这两题思路相似就放在一起. 1852 题意 一块长为L长度单位的板子(从0开始)上有很多只蚂蚁,给出它们的位置,它们的方向不确定,速度为每秒一长度单位,当两只蚂蚁相遇的时候,它们会反向而行,问所有蚂蚁到达终点(到0或者到L)所需要的最短时间和最长时间. 解析 一开始拿到这题觉得还是有点难度的,但一想通就发现这是个大水题.我们可以假设两只蚂蚁它们相向而行,并且它们中间没有别的蚂蚁,此时它们距离为k,经过时间t后它们相遇,此时它们反向,又经过时间t它们再次之间距离还是k.

POJ 1852 Ants

Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15617   Accepted: 6753 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 imm

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 in oppos

POJ 1852 Ants 分析

1.暴搜 每只蚂蚁朝向有两种,可以枚举n只蚂蚁的朝向,然后模拟蚂蚁相遇的情景,总共2^n中情况. 2.分析ants相碰的情况: (a->)  (<-b) 变成 (<-a)(b->) 由于每只蚂蚁是相同的,所以等价与(<-b)(a->),这和两只蚂蚁原来的走向是一样的,即把碰撞当作没发生过 所以可以对每一只蚂蚁检查一次就可以了 3.空间优化 存蚂蚁的初始位置O(n),但是我们每次只要比较 a[i] 和 len-a[i] 就可以了,后面不需要这两个值了,所以完全可以用一个x

POJ 1852 Ants (思维技巧 + 贪心)

Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10639   Accepted: 4718 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 imm

poj 1852 Ants(贪心)

Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14061   Accepted: 6134 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 imm

水题/poj 1852 Ants

1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std; 8 int cmax(int a,int b){return a>b?a:b;} 9 int cmin(int a,int b){return a<b?a:b;} 10 int main() 11 { 12 int cases; 13 scanf("%d",&cas