题干

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.

Example
In:
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191 Out:
4 8
38 207

Ideas

题意大致概括为:

有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都掉下木棍的最短时间和最长时间。

因为能掉头,数据又足足一百万,暴搜显然是不现实的。那么怎么处理这个掉头的问题呢?让我们画个图来理解一下,看完你就会发现这都是吓唬人的。



我们以找最长时间为例,最长的时间可能出现在谁身上呢?通过看图我们知道1号蚂蚁早早下场了,所以最长时间只能出现在2或3身上。2号的路径为2、4、6(标红部分);3号的路径为3、5、7,那么,由于蚂蚁们的速度都相同,我们会发现一个很神奇的规律:

路径1、2、3相等,路程4、5相等,但注意路程6、7不一定相等。所以呢,3号的路程可以写成1、4、7,刚好是1号蚂蚁到右端的距离对不对?同理,2号蚂蚁的路程也可以表示为3,5,6,刚好是3号蚂蚁到左端的距离对不对?以此类推,我们发现其实1,2,3号蚂蚁每个蚂蚁掉下去所走的总距离(即总时间),都能用原来的某一个蚂蚁到两端的距离表示出来(其实第二行中1号蚂蚁到左端还能走一段,但我不想改图了…),无论初始时各个蚂蚁的初方向怎样,规律是不变的。那么当蚂蚁的数量再多一点,这个规律还适用吗?

答案是肯定的:





大家可以试一试,规律依然是适用的。

道理懂了,那么这道题就是白给了。我们直接遍历每个蚂蚁到两端的距离,根据需要取较小或较大的那个,再在所有蚂蚁中找到一个最大的(因为要求所有蚂蚁都掉下去),就是我们的答案。

ps:这题不用开longlong,一百万的数据看着很吓人,但因为没有累加,int就够用了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000000+10;
int a[maxn],len,n;
int main(){
int t;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
len=0;
scanf("%d%d",&len,&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
int minn=0,maxx=0;
for(int i=1;i<=n;i++){
//minn=min(minn,min(a[i],len-a[i])); 注意千万不要这么写,因为我们要保证所有蚂蚁都掉下去,所以括号外是取max的
minn=max(minn,min(a[i],len-a[i]));
maxx=max(maxx,max(a[i],len-a[i]));
}
printf("%d %d\n",minn,maxx);
}
}

[POJ1852] Ants(思维题)的更多相关文章

  1. UVA.10881 Piotr's Ants (思维题)

    UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...

  2. poj1852 Ants ——想法题、水题

    求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...

  3. 思维题 UVA 10881 Piotr's Ants

    题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...

  4. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  5. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  6. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  7. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  8. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  10. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

随机推荐

  1. java关键字tranisent用法详解

    作为java中的一个关键字,tranisent用的并不是很多,但是在某些关键场合,却又起着极为重要的作用,因此有必要对它进行一些必要的了解. 一.定义:声明不用序列化的成员域.(源自百度百科) 二.作 ...

  2. 实现saas多租户方案比较

    看到一篇比较多租户数据隔离方案的文章,总结挺不错.其实大部分内容在我前几年写的文章都有. 文章翻译自: https://blog.arkency.com/comparison-of-approache ...

  3. FastReport分组与聚合

    本来看上去都觉得简单顺便训练下,是想对Customer表中的Company字段以第1个开头的字母分组,结果自己因喜欢将那些东西都集中在一起进行训练,在那个Master-Slave上做例子,并且没用另外 ...

  4. Node.js躬行记(4)——自建前端监控系统

    这套前端监控系统用到的技术栈是:React+MongoDB+Node.js+Koa2.将性能和错误量化.因为自己平时喜欢吃菠萝,所以就取名叫菠萝系统.其实在很早以前就有这个想法,当时已经实现了前端的参 ...

  5. Android学习笔记字符串资源

    在新建好的Android项目里res目录下有个字符串资源文件 在xml文件中引用字符串资源 string.xml <resources> <string name="mot ...

  6. cb51a_c++_STL_算法_根据第n个元素排序nth_element

    cb51a_c++_STL_算法_根据第n个元素排序nth_elementnth_element(b,n,e),比如最大的5个数排序,或者最小的几个数nth_element(b,n,e,p)对比:pa ...

  7. vc6.0打开类向导时报错-Parsing error: Expected ";".Input Line: "解决方法

    --------------------------- Microsoft Visual C++ --------------------------- Parsing error:  Expecte ...

  8. strcmp函数的两种实现

    strcmp函数的两种实现,gcc测试通过. 一种实现: C代码   #include<stdio.h> int strcmp(const char *str1,const char *s ...

  9. 循序渐进VUE+Element 前端应用开发(12)--- 整合ABP框架的前端登录处理

    VUE+Element 前端是一个纯粹的前端处理,前面介绍了很多都是Vue+Element开发的基础,从本章随笔开始,就需要进入深水区了,需要结合ABP框架使用(如果不知道,请自行补习一下我的随笔:A ...

  10. AWS 错误标记3

    1. What is the average queue length recommended by AWS to achieve a lower latency for the 200 PIOPS ...