题干

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. 点击 button 自动刷新页面

    问题:为什么点击 button 会刷新页面 ? 原因:你代码的写法可能如下图,把 <button> 按钮 写在 <form> </form> 标签里边啦. < ...

  2. effictive c++

    c++条款 num 1:尽量以const enum inline替换#define 1)对于单纯常量,最好以const对象或enums替换#defines 2)对于形似函数的宏,最好改用inline函 ...

  3. 遇到Error:Execution failed for task ':app:transformClassesWithDexForDebug'的解决方案

    原因:项目中包含了所有的google play service 解决:只需要使用必要的服务即可 将compile 'com.google.android.gms:play-services:8.1.0 ...

  4. Java学习之第二天

    一.流程控制 1.顺序结构:自上而下,依次执行(从上到下,一直走下去) 2.选择结构:(1)if .if—else.嵌套if (2)switch(mod){ case 1:执行代码 case 2:执行 ...

  5. wdcp如何添加反向代理功能

    1.winscp进入目录 /www/wdlinux/httpd-x.x.x/conf/右键编辑 httpd.conf 这个文件 依次把下面文件名字前面的 # 号去掉 LoadModule proxy_ ...

  6. 最全面的SourceTree账号注册教程

    前言: 作为一个国内开发者而言使用Git操作神器SoureTree最大的问题就是账号注册问题,因为注册账号的链接在不翻墙的情况下基本上是打不开的(弄过的童鞋应该都体会过),所以有的时候我们需要借助一些 ...

  7. qt解决release后数据库连接不上的问题

    问题 : 明明已经设置了 "./xxx" , 为什么release之后数据库还是连不上呢 解决 : 项目中建立一个plugins文件夹 将qt安装目录下的sqldrivers复制到 ...

  8. 关于单向循环链表的约瑟夫问题(Java实现)

    关于单向循环链表的约瑟夫问题(Java实现) 最近在学习链表时,遇到单向循环链表中的约瑟夫问题.在构建循环链表的代码上,我有一点很不理解,遂记录下来. Josephu问题为: 设编号为1, 2,.. ...

  9. 项目实战:Qt手机模拟器拉伸旋转框架

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  10. 一文梳理JavaScript中的this

    最近零零碎碎看了许多关于this的文章,本着"好记性不如烂笔头"的思想,特在这里整理一下this有关的知识点.[长文警告!!!] 接下来,笔者将按照以下目录对this进行阐述: t ...