Ants

来源:互联网 发布:versions for mac使用 编辑:程序博客网 时间:2024/05/16 10:53

1852:Ants

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
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.
输入
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.
输出
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. 
样例输入
210 32 6 7214 711 12 7 13 176 23 191
样例输出
4 838 207
来源
Waterloo local 2004.09.19

这题虽然代码简单,但是挺有意思,两只蚂蚁相撞后分别朝相反方向运动,可以直接看成他们仍然朝原先方向运动,因为到底是哪只蚂蚁在这题并不重要

#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<iomanip>#include<queue>#include<stack>#include<vector>#include<set>#include<map>using namespace std;int main(){int t;cin>>t;while(t--) {int l,n,a,Early=0,Late=0;cin>>l>>n;while(n--){cin>>a;int tmp=min(a,l-a);Early=max(Early,tmp);tmp=max(a,l-a);Late=max(tmp,Late);} cout<<Early<<" "<<Late<<endl;}return 0;}


原创粉丝点击