Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11754   Accepted: 5167

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

很有趣的一道题目,说的是在一个水平线上有很多个蚂蚁,蚂蚁走到端点就下落,蚂蚁两两相碰的话就掉头走,问最少蚂蚁都下落的时间和最多蚂蚁都下落的时间。

因为蚂蚁的速度都相同,所以相遇即是不相遇,完全可以看成是各走各的,不影响。这样思路的话,瞬间就简单很多了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; int main()
{
int test;
cin>>test; while(test--)
{
int length,ant;
int min_val=-1,max_val=-1;
cin>>length>>ant; while(ant--)
{
int dis;
cin>>dis;
min_val=max(min_val,min(dis,length-dis));
max_val=max(max_val,max(dis,length-dis));
}
cout<<min_val<<" "<<max_val<<endl;
}
return 0;
}

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

最新文章

  1. 安装MariaDB和简单配置
  2. Caused by: java.lang.UnsatisfiedLinkError: Couldn&#39;t load BaiduMapVOS_v2_1_3: findLibrary returned nu
  3. 【CodeVS 2822】爱在心中
  4. win7,win8.1下hosts文件无法修改的快速解决办法
  5. mybatis学习之路
  6. haproxy.cfg
  7. python的使用环境总结
  8. Linux内存分析
  9. 怎样在Windows和Linux下写相同的代码
  10. Andrid 高级程序员面试题
  11. [django1.6]跑批任务错误(2006, &#39;MySQL server has gone away&#39;)
  12. 整理+学习《骆昊-Java面试题全集(中)》
  13. 【FJWC 2019】min
  14. mysql锁2
  15. Important persons in deep learning
  16. JAVA记录-基础常识
  17. 比beanutil更加灵活的dto转换工具dozer
  18. sqler sql 转rest api 源码解析(四)macro 的执行
  19. YII2中如何自定义全局函数
  20. Windows server 2008 r2 如何开启Win7的Aero效果 (转)

热门文章

  1. 「Luogu P5494 【模板】线段树分裂」
  2. 「CH6901」骑士放置
  3. unittest---unittest数据驱动(ddt)
  4. 二 SSH整合:Spring整合Hibernate,无障碍整合&amp;无核心配置整合,Hibernate模版常用方法,
  5. Linux下,Tomcat启动成功,发现ip:8080访问失败
  6. kafka在zookeeper默认使用/为根目录,将/更换为/kafka
  7. httpclient访问接口步骤
  8. Unity3d fbx纹理不显示 原因
  9. HTML&lt;a&gt;标签作为锚链接
  10. Bar Chart _Study