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. C语言--矩阵置换

    //takePlace里的循环写错了,j循环应该是 //for (j=i;j<3;j++) //你那个写的交换了2遍,又变回原来的了.*// #include <stdio.h> ] ...

  2. Subl 命令

    Subl 是sublime 的命令 添加环境变量后可以,在cmd 或者git 下直接 使用subl 进行 打开sublime Example:     Subl 打开编辑器     Subl . 将当 ...

  3. redux-actions

    其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...

  4. (一)Oracle学习笔记—— 表和表空间

    1. 表空间 一个数据库可以有多个表空间,一个表空间里可以有多个表.表空间就是存多个表的物理空间:可以指定表空间的大小位置等.  1.1 创建表空间语句 create tablespace ts3 d ...

  5. [1-2] 把时间当做朋友(李笑来)Chapter 2 【开启自己的心智】 摘录

    心智是可培养的.可发展的.甚至是可以重建的 早意识到,早些培养可以让自己起步更早些 审视一下我们自己,运用自己的心智,我们会知道每个人可以把自己划分为两部分:自己知道的与自己并不知道的.我们有的时候并 ...

  6. 查看电脑CPU核心数的方法

    查看电脑CPU核心数的方法: 方法一: 同时按下[Ctrl+Shift+Esc]组合快捷键打开任务管理器: 点击[性能]就可以看出是几核CPU了: 方法二: 在计算机图标上面点击右键,选择“管理”: ...

  7. genymotion启动黑屏的原因及临时解决方法

    解决办法 好像是要激活网络,具体原因不知道!!! sudo ifconfig vboxnet0 up 下面好像更好使一点 sudo ip link set dev vboxnet0 up

  8. shell脚本与mongo交互

    1.mongo提供*.js的方法供linux调用 #!/bin/bash /usr/local/mongodb/bin/mongo hostname/dbname xxx.js xxx.js 内容如下 ...

  9. [ci] 基于1 上文实现拉取代码后能自动触发sonar-runner实现代码扫描评测,job1完成

    基于1 上文实现拉取代码后能自动触发sonar-runner实现代码扫描评测,job1完成   添加sonar插件 SonarQube Plugin   配置: 系统设置à告知jenkins,sona ...

  10. Unity插件之NGUI学习(4)—— 创建UI2DSprite动画

    创建一个新的Scene.并按 Unity插件之NGUI学习(2)创建UI Root,并在UI Root的Camera下创建一个Panel. 然后在选中Panel,在菜单中选择NGUI->Crea ...