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 ...
随机推荐
- 第一章03-Activity的启动模式
Activity的LaunchMode Android中提供了四中Activity的启动模式 1. standard 2. singleTop 3. singleTask 4. signleInsta ...
- RocketMQ系列(四)顺序消费
折腾了好长时间才写这篇文章,顺序消费,看上去挺好理解的,就是消费的时候按照队列中的顺序一个一个消费:而并发消费,则是消费者同时从队列中取消息,同时消费,没有先后顺序.RocketMQ也有这两种方式的实 ...
- 如何在Centos7安装swoole的PHP扩展
1. 下载swoole源代码包 wget -c https://github.com/swoole/swoole-src/archive/v2.0.8.tar.gz 2.tar -zxvf v2.0. ...
- Github即将破百万的PDF:编写高质量代码改善JAVA程序的151个建议
在通往"Java技术殿堂"的路上,本书将为你指点迷津!内容全部由Java编码的最佳 实践组成,从语法.程序设计和架构.工具和框架.编码风格和编程思想等五大方面,对 Java程序员遇 ...
- Brainman(规律题)【数学思想】
Brainman 题目链接(点击) Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12942 Accepted: 650 ...
- (一)Log4j使用
原文链接:https://www.jianshu.com/p/eb4ac2571c94?tdsourcetag=s_pctim_aiomsg 1.先创建个maven项目,在我们项目的pom文件中导入l ...
- MFC_VC++_时间获取与保存列表控件内容到文件操作方法
MFC_VC++_时间获取与保存列表控件内容到excel文件操作方法 void CDataView::OnBnClickedBtnExporttoexcel() { CTime time = CTim ...
- cb52a_c++_STL_堆排序算法make_push_pop_sort_heap
cb52a_c++_STL_堆排序算法make_push_pop_sort_heapheapsort堆排序算法make_heap()-特殊的二叉树,每一个节点都比根小,根就是最大的数.大根堆,也可以做 ...
- 05.DRF-Django REST framework 简介
一.明确REST接口开发的核心任务 分析一下上节的案例,可以发现,在开发REST API接口时,视图中做的最主要有三件事: 将请求的数据(如JSON格式)转换为模型类对象 操作数据库 将模型类对象转换 ...
- 容器技术之Docker Machine
前文我们聊了下docker容器的资源限制,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13138725.html:今天我们来聊一聊docker machine ...