地址  http://poj.org/problem?id=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 Sample Output

算法1

两只蚂蚁碰头后就各自回头 其实是一个思维陷阱, 它与两只蚂蚁碰头后就擦身而过是完全一样的
那么只要计算每次蚂蚁的最小路径选择与最大路径选择即可

#include <iostream>
#include <algorithm> using namespace std; /*
Sample Input 2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
Sample Output 4 8
38 207 */
#define MAX_NUM 999999 int ants[MAX_NUM]; void Do(int ants[],int len,int num)
{
int longLen =, shortLen = ;
for (int i = ; i < num; i++) {
longLen = max(longLen, max(ants[i], len - ants[i]));
shortLen = max(shortLen, min(ants[i], len - ants[i]));
} cout << shortLen << " " << longLen << endl;
} int main()
{
int n = ;
cin >> n;
for (int i = ; i < n; i++) {
int len = ; int num = ;
cin >> len >> num;
memset(ants,,sizeof(ants));
for (int j = ; j < num; j++) {
cin >> ants[j];
}
Do(ants,len,num); }
}

最新文章

  1. http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html
  2. URAL 1658. Sum of Digits(DP)
  3. [Winform]一个简单的账户管理工具
  4. Java基础(45):冒泡排序的Java封装(完整可运行)
  5. easyUI dialog 弹窗 居中显示
  6. ADO.NET Entity Framework(EF)
  7. Python标准库10 多进程初步 (multiprocessing包)
  8. 推荐第三方Oracle客户端查询工具
  9. ES6学习笔记(十四)
  10. windows 文件名太长无法删除的解决方法
  11. Python识别网站验证码
  12. visual studio错误中断处理
  13. Java开发利器--Lombok,IDEA端安装教程
  14. pig简单的代码实例:报表统计行业中的点击和曝光量
  15. Azure Database for MySQL 报 Please specify SSL options and retry.
  16. vn.trader的Ubuntu运行环境搭建教程
  17. java框架之Spring(4)-Spring整合Hibernate和Struts2
  18. 数字签名-MD5
  19. (mysql)触发器、事件、事务、函数
  20. [SublimeText] 之 Packages

热门文章

  1. C#深入浅出之操作符和控制流程
  2. PlayJava Day029
  3. 基于Git的数据库sql文件的管理——完美解决团队sql操作协同问题
  4. Mac环境下执行npm install报权限错误解决办法
  5. Harbor 清理镜像(此方法比较粗暴,但是有效)
  6. maven多仓库配置 公司仓库和阿里仓库
  7. 因果推理的春天系列序 - 数据挖掘中的Confounding, Collidar, Mediation Bias
  8. combination sum &amp;&amp; combination sum II
  9. 记一次排查jacoco的过程:java.lang.NoSuchMethodException:ApplyOrderdetail.get$jacocoData()
  10. JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离