POJ1852 Ants 题解
题目
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)\}\)
注意所有蚂蚁掉下来的时间等于最后一只蚂蚁掉下来的时间,所以所有蚂蚁的距离中取最大值
对于每只蚂蚁:
最长时间取最大值,我们可以认为每只蚂蚁都很傻,选择最长的路走,然后最后一只蚂蚁掉下来的时间就是答案
最短时间取最小值,我们可以认为每只蚂蚁都很聪明,选择最短的路走,然后最后一只蚂蚁掉下来的时间就是答案
代码
#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 题解的更多相关文章
- poj1852 Ants ——想法题、水题
求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...
- [POJ1852]Ants
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12431 Accepted: 5462 Description An a ...
- poj1852 Ants(思维)
https://vjudge.net/problem/POJ-1852 最短时间显然是各自往靠近端点的地方走. 最长时间关键是要想到,折返和擦肩其实是一样的,可以当成两只蚂蚁换了个位子,最终求max是 ...
- poj 1852 ants 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...
- [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 ...
- [POJ3684]Physics Experiment
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1363 Accepted: 476 Special Judge ...
- 【题解】Luogu UVA1411 Ants
原题传送门 博客里对二分图匹配的详细介绍 这道题是带权二分图匹配 用的是KM算法 我们要知道一个定理:要使线段没有相交,要使距离总和最小 我们先把任意一对白点.黑点的距离算一下 然后运用KM算法 因为 ...
- 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 ...
- 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 ...
随机推荐
- 服务端监控工具Nmon使用方法
一.认识nmon 1.简介 nmon是一种在AIX与各种Linux操作系统上广泛使用的监控与分析工具,它能在系统运行过程中实时地捕捉系统资源的使用情况,记录的信息比较全面, 并且能输出结果到文件中,然 ...
- python—列表,元组,字典
——列表:(中括号括起来:逗号分隔每个元素:列表中的元素可以是数字,字符串,列表,布尔值等等) (列表元素可以被修改) list(类) (有序的) [1]索引取值:切片取值:for循环:whi ...
- Linux创建与删除
一.创建文件夹.文本.用户.组 mkdir 创建文件夹 touch 创建文本 useradd 创建用户 例1:创建一个文件夹xiaomi 创建文件夹:mkdir xiaomi 例2:创建一个文件如ad ...
- 本地配置gitee
一 下载工具 Git-2.62.0-64-bit.exe 以上工具版本号不需要一样,安装完前两个后重新启动系统,再安装第3个. 二 码云网站注册 https://gitee.com/ 使用邮箱注册 注 ...
- 【asp.net core 系列】8 实战之 利用 EF Core 完成数据操作层的实现
0. 前言 通过前两篇,我们创建了一个项目,并规定了一个基本的数据层访问接口.这一篇,我们将以EF Core为例演示一下数据层访问接口如何实现,以及实现中需要注意的地方. 1. 添加EF Core 先 ...
- 3、react-props/state
1.react中属性props和状态state 属性--静态得,所以在初始化得时候使用得是static进行初始化得,正常情况下属性不改 状态--动态得,它得值是可以发生改变得,react中的组件更新( ...
- el-table 表格加图片、加音频、加序号、多级动态表头
elemnet-ui组件库大家应该不陌生,在展示多条结构类似的数据方面,el-table可谓扛把子,不仅可以把数据展示的整齐,还支持排序.筛选或其他自定义操作.那么,除了上述的基本功能外,你还遇到过哪 ...
- getline使用问题
1.输入string string s1; getline(cin,s1); cin>>s1; //注意cin遇到空格会终止,而getline不会 2.关于吞回车问题 输入n后要记得吞回车 ...
- MySQL数据库离线包安装与注册
本文主要介绍了MySQL数据库的离线安装和将MySQL服务注册为Windows应用服务的主要步骤. 1.下在安装程序包 MySQL Community Server 5.6.15 官方下载地址http ...
- SuperSlide之属性targetCell介绍
禁用页面菜单js代码: <script type="text/javascript"> document.oncontextmenu=function(){ retur ...