Ants(思维)

Ants

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12893   Accepted: 5637

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题解:水题,就是一个木棒在水里,蚂蚁在这个木棒上可以右走,也可以左走,1cm/s,问最小最大多长时间都掉到水里;代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=1000010;
int a[MAXN];
int main(){
	int T,l,n,x,mx;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&l,&n);
		mx=-1;
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			mx=max(mx,x);
			mx=max(mx,l-x);
			a[i]=min(x,l-x);
		}
		printf("%d %d\n",*max_element(a,a+n),mx);
	}
	return 0;
}

  

时间: 2024-10-26 03:13:16

Ants(思维)的相关文章

UVa10881 Piotr&#39;s Ants (思维)

链接:http://acm.hust.edu.cn/vjudge/problem/25979分析:n只蚂蚁在爬,将n只蚂蚁距离木棍左端的距离从小到大排序后它们的相对顺序是不变的,因为碰到另一只蚂蚁两只蚂蚁就会掉头,蚂蚁就像一个弹珠来回弹,但整体上“掉头”等价于“对穿而过”,但题目要求输入时的顺序输出,所以并不是所有蚂蚁都是一样的,还要认清谁是谁,所以如果蚂蚁1初始状态为(3,R)两秒后在(5,R)的位置会出现一只蚂蚁但不一定是蚂蚁1,但是可以肯定(5,R)位置上的蚂蚁相对于其它蚂蚁的顺序和初始状

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 (思维题)

n只蚂蚁以每秒1cm的速度在长为Lcm的杆子上爬行,当蚂蚁爬到杆子的端点时就会掉落,由于杆子太细,两只蚂蚁相遇时,他们不能交错通过,只能各自反向爬回去,对于每只蚂蚁,我们知道它距离杆子左端的距离为x,但不知道它当前的朝向,请计算所有蚂蚁落下杆子所需的最短时间很最长时间. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring&g

UVA 10881 - Piotr&#39;s Ants【模拟+思维】

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822 题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向.并且当相邻两个蚂蚁相撞时转向.现在问t时间后各个蚂蚁的位置. 解法:这题的一个致命技巧就是把两只蚂蚁的相撞看作是两只蚂蚁交换穿过对方并且交换蚂蚁的编号.这个是很好理解的,类似于物理的完全弹性碰撞.又由

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

物联网世界常见传输方式简介(思维导图)

物联网世界常见的传输方式包括:移动网络(2G/3G/4G).wifi.蓝牙.ZigBee等传统的传输方式,以及基于NB-IOT.Lora等新兴的传输方式.以下思维导图供参考: 点击附件下载原图.

贝叶斯思维漫步

现在仍然记得大学最"无聊"的一堂课之一--概率论,出勤人数三个班加起来也没超过正常一个班的数量,当然最后一堂课除外(笑).个人感觉上课也比较枯燥,当时完全不知道概率论可以用在什么方面,所有听课也就不是那么认真,结果就是期末考试只有70多分(想想当年高数90多线性代数也90······).然而随着大学毕业,概率论也就离我远去,好像不会再有交集.后来开始"专研"机器学习方面的知识,"朴素贝叶斯"这个名词映入我的眼帘,遥远的记忆才被唤起,记得概率论中有

zoj 3672 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4915 真是老了,脑子不会动了,但是其实就算现在搜了题解A了,还是没总结出思维方式 三点: 1.segma(a[i]-b[i])必须是偶数,,因为其实每次操作都是相当于从segma(a[i]-b[i])里面减去2*delta 2.a[i]>=b[i] 题目说的很清楚,只能减去,所以这点必须满足 前两点都想到了,但是自己能举出反例,后来队友A掉了 3.max(a[i]-b[i])

Node 即学即用 笔记 思维导图

Node即学即用 REPL(Read-Evaluate-Print-Loop) console.log .clear .help .exit require('http') createServer 聊天服务器 tcp服务器 require('net') on connection on data on end on error telnet express app.get app.post -app.js -public -views ----partials EJS模板语言 测试 requi