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 ...
随机推荐
- 基于使用ISCSI存储的ibmmq通过heartbeat实现HA方案以及碰到的问题总结
一.背景 ibmmq是一种传统架构的mq产品,运行稳定,有其自身优点,但在高可用(HA)这一块需要使用公司根据自身需求选用高可用(HA)产品,但由于市面HA商业产品较贵,所以使用linux操作系统级的 ...
- 单例模式之懒汉模式,懒汉模式之高效模式,DLC双判断模式
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; imp ...
- String源码理解之indexOf函数
1前言 不多说,直接上源码 2源码 我自己的理解,可能表述不清,多看几遍,不行就debug跟一遍代码自然就懂了. /** * Code shared by String and StringBuffe ...
- @loj - 2004@ 「SDOI2017」硬币游戏
目录 @description@ @solution@ @accepted code@ @details@ @description@ 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数 ...
- PyQt5常用控件及方法(代码段)
文章目录PyQt5程序基本格式QMainWindow主窗口QLabel标签QTextBrowser文本浏览器QTextEdit文本编辑器QPushButton按键QCheckBox复选框QRadioB ...
- 解决GITLAB无法启动runsv no running
可以先尝试用 systemctl start gitlab-runsvdir.service 如果冻结不动的话,就需要查看系统服务项了 systemctl -t target 如果系统正常的话,所有任 ...
- (二)POI-创建一个sheet页,并添加行列数据
原文:https://blog.csdn.net/class157/article/details/92800439 1.只创建sheet页 package com.java.poi; import ...
- MyISAM和InnoDB的差异
转载:https://www.cnblogs.com/xc-chejj/p/11245034.html MyISAM是默认表类型,基于ISAM(Indexed Sequential Access Me ...
- 8种排序算法 Java实现
冒泡排序 O(n2) 两个数比较大小,较大的数下沉,较小的数冒起来. public static void bubbleSort(int[] a) { //临时变量 int temp; //i是循环次 ...
- cb26a_c++_STL_算法_元素计数
cb26a_c++_STL_算法_元素计数所有容器都可以使用countcount_if关联容器的等效成员函数,容器自己的成员函数速度较快1.set.count2.multiset.count3.map ...