【弹性碰撞问题】POJ 1852 Ants
Description
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.
Sample Input
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
Sample Output
4 8
38 207
题意
有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最短时间和最长时间。
分析
看到这个题,一开始的想法应该都是直接暴搜氵分,但是注意一下数据范围,肯定是过不了的,所以要换一种思想。 因为是同时出发的,相遇时的两只蚂蚁用的时间是相同的,我们可以无视蚂蚁的区别,当两只蚂蚁相遇时它们保持原样交错而行。这样每只蚂蚁都是独立运动的,那么只要找每只蚂蚁掉下去的时间就行了。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e6+;
int a[maxn],ansx,ansd,L,n;
void ans1(){
int Min;
ansx = -;
for(int i=;i<n;++i){
Min = min(a[i],L-a[i]);
if(Min>ansx)
ansx = Min;
}
printf("%d ",ansx);
}
void ans2()
{
int Max;
ansd=-;
for(int i=;i<n;++i)
{
Max=max(a[i],L-a[i]);
if(Max>ansd)
ansd=Max;
}
printf("%d\n",ansd);
} int main(){ int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&L,&n);
for(int i=;i<n;++i)
scanf("%d",&a[i]);
ans1();//求所有蚂蚁掉下去的最短时间
ans2();//求所有蚂蚁掉下去的最长时间
}
return ;
}
【弹性碰撞问题】POJ 1852 Ants的更多相关文章
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- POJ 1852 Ants(贪心)
POJ 1852 Ants 题目大意 有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最 ...
- poj 1852 ants 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...
- POJ 1852 Ants
题目的意思是说一个长度为m的杆,上面有n个蚂蚁,告诉每个蚂蚁的初始位置,每个蚂蚁速度都是一样的,问所有的蚂蚁离开杆的最短和最长时间是多少. 模拟题,所有的蚂蚁看成一样的,可以这样理解,即使相撞按反方向 ...
- POJ 1852 Ants (等价思考)
题意:在一根杆上有 n 只蚂蚁,速度为1,方向不定,如果相碰,则反向运动,问你最长的时间和最短时间,所有蚂蚁都掉下杆去. 析:换个方法想,如果两只蚂蚁相碰了,会有什么现象?其实就和没有碰撞是一样的,没 ...
- POJ 1852 Ants O(n)
题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...
- 【纯水题】POJ 1852 Ants
题目大意 有一根长\(L\)厘米米的水平木棍上有\(n\)个蚂蚁,它们以每秒1cm/s的爬(fei)行(ben)到木棍的一端,之后掉下去. 给出每个蚂蚁的起始位置,但是不知道它们爬行的方向.相向而行的 ...
- poj 1852&3684 题解
poj 1852 3684 这两题思路相似就放在一起. 1852 题意 一块长为L长度单位的板子(从0开始)上有很多只蚂蚁,给出它们的位置,它们的方向不确定,速度为每秒一长度单位,当两只蚂蚁相遇的时候 ...
- Ants (POJ 1852)
题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...
随机推荐
- 【Flume】知识总结
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...
- Java 第十一届 蓝桥杯 省模拟赛 无向连通图最少包含多少条边
无向连通图最少包含多少条边 题目 问题描述 一个包含有2019个结点的无向连通图,最少包含多少条边? 答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填 ...
- Java实现蓝桥杯模拟递增三元组
问题描述 在数列 a[1], a[2], -, a[n] 中,如果对于下标 i, j, k 满足 0<i<j<k<n+1 且 a[i]<a[j]<a[k],则称 a ...
- 第二届蓝桥杯C++B组国(决)赛真题
以下代码仅供参考,解答部分来自网友,对于正确性不能保证,如有错误欢迎评论 四方定理. 数论中有著名的四方定理:所有自然数至多只要用四个数的平方和就可以表示. 我们可以通过计算机验证其在有限范围的正确性 ...
- maven配置阿里云仓库进行下载
maven阿里云仓库下载 为了解决maven在下载jar包的时候,速度比较慢的问题,可以配置阿里云仓库配置方式的进行下载,首先找到您安装的maven路径. 在conf文件夹下面有个settings.x ...
- 温故知新-多线程-深入刨析volatile关键词
文章目录 摘要 volatile的作用 volatile如何解决线程可见? CPU Cache CPU Cache & 主内存 缓存一致性协议 volatile如何解决指令重排序? volat ...
- MySQL 8.0 yum安装和配置
MySQL 8.0 centos7.5 x86_64 一.yum安装 1.先卸载机器和mysql有关的东西,有的安装了mariab-lib,会对安装有干扰,卸载了它. [root@localhost ...
- Spyder汉化教程
汉化包下载地址:https://www.lizenghai.com/archives/523.html 1.解压汉化包 2. 3.1.运行汉化补丁PS C:\WINDOWS\system32> ...
- 关于thinkphp5下URL附加参数,无法获取到(?参数)
nginx 配置问题: 修改配置后:
- @loj - 3043@「ZJOI2019」线段树
目录 @description@ @solution@ @accepted code@ @details@ @description@ 九条可怜是一个喜欢数据结构的女孩子,在常见的数据结构中,可怜最喜 ...