Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12782   Accepted: 5596

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

Source

思路:

大意是蚂蚁在杆子上爬行,方向未知,如果相遇时会各自反向爬回去。计算所有蚂蚁落下杆子所需的最短时间和最长时间。

穷竭算法比较容易想到,枚举所有蚂蚁初始朝向的组合然后验证,复杂度为2^N,显然太高了。

其实这个题有比较巧的思路,蚂蚁相遇时两只蚂蚁若交换,就好像两个蚂蚁仍然按照自己原来的路线走一样。所以压根不需要考虑相遇而进行多次模拟。

那么其实只要检查两端的两只蚂蚁即可。

代码:

Problem: 1852		User: liangrx06
Memory: 740K Time: 141MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int main(void)
{
int t, n, len;
int pos, minT, maxT; cin >> t;
while (t--)
{
cin >> len >> n;
maxT = 0;
minT = 0;
while (n--)
{
scanf("%d", &pos);
maxT = max(maxT, max(pos, len-pos));
minT = max(minT, min(pos, len-pos));
}
printf("%d %d\n", minT, maxT);
} return 0;
}

《挑战程序设计竞赛》1.6 轻松热身 POJ1852的更多相关文章

  1. Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

    King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...

  2. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  3. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...

  4. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  5. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  6. 《挑战程序设计竞赛》 4.1.1 矩阵 P286

    想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...

  7. poj1182食物链_并查集_挑战程序设计竞赛例题

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65534   Accepted: 19321 Description ...

  8. 迷宫问题_BFS_挑战程序设计竞赛p34

    给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...

  9. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

随机推荐

  1. 大巧不工web前端设计修炼之道—笔记

      设计原则: 深入人心的设计--别让我思考 简洁是一种文化,一种需求,一种思想   ·排版 ·字体(衬线 | | 无衬线)

  2. android 完美的tabhost 切换多activity布局

    TabHost在很多应用都会使用到,有时候在TabHost添加的Tab中设置view不能满足需求,因为在view中添加如PreferenceActivity相当困难. 之前在一个应用中需要实现使用Ta ...

  3. Java 8 Lambda表达式介绍

    Lambda是什么? Lambda是一个匿名函数,我们可以把Lambda理解为是一段可以传递的代码.可以写出简洁.灵活的代码.作为一种更紧凑的代码风格,使java的语言表达能力得到提升. 可以这么说l ...

  4. Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持

    Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持 1. 设置 document.domain为一致  推荐1 2. Apache 反向代理 推荐1 ...

  5. mysql创建数据库时设置编码方式

    CREATE DATABASE procedure_function DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  6. [转]jsonp详解

    jsonp详解 json相信大家都用的多,jsonp我就一直没有机会用到,但也经常看到,只知道是“用来跨域的”,一直不知道具体是个什么东西.今天总算搞明白了.下面一步步来搞清楚jsonp是个什么玩意. ...

  7. 利用JNDI的命名与服务功能来满足企业级API对命名与服务的访问

    包含了大量的命名和目录服务,使用通用接口来访问不同种类的服务: 可以同时连接到多个命名或目录服务上: 建立起逻辑关联,允许把名称同Java对象或资源关联起来,而不必知道对象或资源的物理ID. JNDI ...

  8. python 动态语言 __slots__

    python 是动态语言,就是说可以动态的创建属性, 别的语言不行,再创建类的时候已经规定好了 使用__slots__,注意要用tuple定义同意绑定的属性名称,仅对当前类起作用,对继承的子类是不起作 ...

  9. 多媒体开发之音频编码---ffmpeg 编码aac

    http://blog.csdn.net/ctroll/article/details/8169396

  10. DOM4J操作XML(包括封装和解析)

    DOM4J操作XML(包括封装和解析) 先来看看dom4j中相应XML的DOM树建立的继承关系 针对于XML标准定义.相应于图2-1列出的内容,dom4j提供了下面实现: 以下给出一个详细事例: pa ...