题目

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.

一群蚂蚁在长度为\(1cm\)的水平杆上行走,每只速度为\(1 cm/s\)。 当蚂蚁到达杆的一端时,它立即从杆上掉下来。当两只蚂蚁相遇时,他们转身开始向相反的方向行走。 我们知道蚂蚁在杆子上的原始位置,不幸的是,我们不知道蚂蚁的行走方向。 您的任务是计算所有蚂蚁掉下来所需的最长和最短的时间。

输入格式

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.

输入数据包含多组,输入的第一行包含一个整数,给出随后的组数。 每组的数据均以两个整数开头:杆的长度(以厘米为单位)和 \(n\)(杆上的蚂蚁数)。后面紧跟着\(n\)个整数,给出了每个蚂蚁在杆上的位置,从杆左端开始的距离(无特定顺序)。 所有输入整数均不大于1000000,并且它们之间用空格分隔。

输出格式

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.

对于每种输入情况,输出两个数字,并用一个空格隔开。 第一个数字是所有蚂蚁掉下来的最短时间(如果正确选择了它们的行走方向),第二个数字是最长时间。

题解

这道题很简单,由于两只蚂蚁相遇立即转头,和不考虑这个条件,两只蚂蚁相遇时互相穿过是等价的,所以直接考虑每只蚂蚁自己即可.

由于每只蚂蚁速度是\(1 m/s\),所以时间数值上等于距离,我们只需要考虑距离即可

设第i只蚂蚁距离左端\(L_i\),距离右端\(R_i\)

最长时间等于\(max\{max (L_i,R_i)\}\)

最短时间等于\(max\{min (L_i,R_i)\}\)

注意所有蚂蚁掉下来的时间等于最后一只蚂蚁掉下来的时间,所以所有蚂蚁的距离中取最大值

对于每只蚂蚁:

  1. 最长时间取最大值,我们可以认为每只蚂蚁都很傻,选择最长的路走,然后最后一只蚂蚁掉下来的时间就是答案

  2. 最短时间取最小值,我们可以认为每只蚂蚁都很聪明,选择最短的路走,然后最后一只蚂蚁掉下来的时间就是答案

代码

#include<cstdio>
#include<algorithm>
inline int input(){int t;scanf("%d",&t);return t;}
int main(){
for(int t=input();t--;){
int l=input(),n=input(),maxv=0,minv=0,temp;
for(int i=1;i<=n; i++)
temp=input(),maxv=std::max(maxv,std::max(temp,l-temp)),minv=std::max(minv,std::min(temp,l-temp));
printf("%d %d\n",minv,maxv);
}
}

POJ1852 Ants 题解的更多相关文章

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

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

  2. [POJ1852]Ants

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12431   Accepted: 5462 Description An a ...

  3. poj1852 Ants(思维)

    https://vjudge.net/problem/POJ-1852 最短时间显然是各自往靠近端点的地方走. 最长时间关键是要想到,折返和擦肩其实是一样的,可以当成两只蚂蚁换了个位子,最终求max是 ...

  4. poj 1852 ants 题解《挑战程序设计竞赛》

    地址  http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...

  5. [POJ1852] Ants(思维题)

    题干 An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. W ...

  6. [POJ3684]Physics Experiment

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1363   Accepted: 476   Special Judge ...

  7. 【题解】Luogu UVA1411 Ants

    原题传送门 博客里对二分图匹配的详细介绍 这道题是带权二分图匹配 用的是KM算法 我们要知道一个定理:要使线段没有相交,要使距离总和最小 我们先把任意一对白点.黑点的距离算一下 然后运用KM算法 因为 ...

  8. Educational Codeforces Round 7 E. Ants in Leaves 贪心

    E. Ants in Leaves 题目连接: http://www.codeforces.com/contest/622/problem/E Description Tree is a connec ...

  9. Codeforces Round #188 (Div. 1) B. Ants 暴力

    B. Ants Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/317/problem/B Des ...

随机推荐

  1. 因为 MongoDB 没入门,我丢了一份实习工作

    有时候不得不感慨一下,系统升级真的是好处多多,不仅让我有机会重构了之前的烂代码,也满足了我积极好学的虚荣心.你看,Redis 入门了.Elasticsearch 入门了,这次又要入门 MongoDB, ...

  2. 在MyEclipse中设置jdk

    在MyEclipse中设置jdk的三处地方:1 选中项目右键菜单properties -->java Compiler 2 windows菜单中Preferences-->myeclips ...

  3. Android官方新推的DI库 Hilt

    Android官方新推的DI库 Hilt Hilt是Google Android官方新推荐的依赖注入工具. 已加入到官方文档: Dependency injection with Hilt. 目前是a ...

  4. Centos6,Centos7防火墙设置与端口开放的方法

    Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld.端口的开启还是要从两种情况来说明的,即iptables和firewalld. 一.iptables 1.打开/关闭 ...

  5. [CF696D]Legen...

    题目   点这里看题目. 分析   首先对于模式串建立 AC 自动机,并且计算出每个状态\(p\)的贡献总和\(con(p)\).   考虑一个朴素的 DP :   \(f(i,p)\):当前串长度为 ...

  6. [AGC043-D]Merge Triplets

    题目   点这里看题目. 分析   我们不妨来考虑一下生成的序列有什么性质.   为了方便表示,我们将序列\(S\)的第\(i\)项写为\(S[i]\).   首先考虑如果所有的\(A\)序列都是递增 ...

  7. 包子凑数(dp 0-1、完全背包)【背包问题】

    包子凑数(蓝桥杯) 感谢:@ Statusrank 题目链接(点击) 题目描述 小明几乎每天早晨都会在一家包子铺吃早餐.他发现这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子.每种蒸笼都有非常多 ...

  8. Arduino_DH11+0.96OLED_u8glib库 温湿度显示

    u8glib_arduino_OLED0.96一.电路连接 DHT11接线方式:GND<————>GNDDATA<————>2(数字输入/输出)VCC<————>5 ...

  9. 商城02——dubbo框架整合_商品列表查询实现_分页

    1.   课程计划 1.服务中间件dubbo 2.SSM框架整合. 3.测试使用dubbo 4.后台系统商品列表查询功能实现. 5.监控中心的搭建 2.   功能分析 2.1. 后台系统所用的技术 框 ...

  10. Flink Table Api & SQL 初体验,Blink的使用

    概述 Flink具有Table API和SQL-用于统一流和批处理. Table API是用于Scala和Java的语言集成查询API,它允许以非常直观的方式组合来自关系运算符(例如选择,过滤和联接) ...