POJ1852 Ants

目录

  • 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.

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 191Sample Output
4 8
38 207

题意

有一根长为Lcm的水平杆,一群蚂蚁以1cm/s的速度爬行在这根杆上。当一只蚂蚁爬行到杆的端点时立即下落,当两只蚂蚁相遇时,立即各自调头往回走。我们知道每一只蚂蚁的初始位置,但是我们不知道他们的初始方向。你的任务是计算所有蚂蚁都掉落水平杆的最早和最晚的时间。

输入第一行为一个整数T,代表共有T个样例。
每个样例第一行为两个整数,len为水平杆的长度,n为蚂蚁数量。
第二行为n个整数,代表第 i 个蚂蚁的初始位置。(所有的数据都不超过)

对于每一个样例的输出为两个整数,分别为最早和最晚时间。

题解

我在刚开始看到这道题的时候感觉有点无从下手,第一反应就是直接模拟暴力求解,后来觉得这样做必然超时啊,也没有去算复杂度。
然后翻到页面下方有一个dalao评论说忽视蚂蚁的区别,妙极。
因为蚂蚁自身除了方向不同是没有区别的,然后它们相遇之后反向而行可以理解为相遇之后继续向前,这道题就变成个一道知道各个点的位置求最快最慢下落时间的题了。上代码。

https://vjudge.net/problem/POJ-1852

#include<iostream>
using namespace std;
int main()
{int T;cin >> T;int L, n;while(T--){cin >> L >> n;int x, lon = -1, shor = -1; //初始化最长最短时间for(int i = 0; i < n; i++){cin >> x;if(x <= L/2){  //当前位置距离起始端比较近,lon = max(lon, L-x); //最长时间自然要走到尾端落下,所以是L-xshor = max(shor, x);  //因为要所有的蚂蚁全都下落所以两个都是max函数}else{lon = max(lon, x);shor = max(shor, L-x);}}cout << shor << " " << lon << endl;}return 0;
}

明明看dalao的代码都是前面带行号的
新手求支持

【题解】POJ 1852 Ants(搜索)相关推荐

  1. poj 1852 Ants

    题目:http://poj.org/problem?id=1852 本题如果从常规的思想出发去解决问题是比较复杂的,而且时间复杂度会比较高,极有可能超时,但本书给出了一个非常巧妙的解法,程序简单易懂, ...

  2. POJ 1852 Ants 分析

    1.暴搜 每只蚂蚁朝向有两种,可以枚举n只蚂蚁的朝向,然后模拟蚂蚁相遇的情景,总共2^n中情况. 2.分析ants相碰的情况: (a->)  (<-b) 变成 (<-a)(b-> ...

  3. 水题/poj 1852 Ants

    1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...

  4. POJ 1852 Ants O(n)

    题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...

  5. 二分图最佳匹配 KM算法 Hdu2255奔小康赚大钱 + Poj 3565 Ants

    2014-10-4 更新 在最下面增加了基于邻接表的模板 理论:http://blog.sina.com.cn/s/blog_691ce2b701016reh.html http://philosci ...

  6. Ants(POJ 1852)

    题目大意:一个蚂蚁群在一个杆子上爬,爬到杆子的末端时就会掉落,如果两只蚂蚁碰面,那么两只蚂蚁会反向走,问所有蚂蚁掉落的最短时间和最长时间.我们已知杆子的长度和蚂蚁的数量和每只蚂蚁的位置(即其距离杆子左 ...

  7. [题解]POJ 3683 Priest John's Busiest Day

    [Description] John is the only priest in his town. September 1st is the John's busiest day in a year ...

  8. 题解 POJ 2559-SP1805 【HISTOGRA - Largest Rectangle in a Histogram】

    题目链接: https://www.luogu.org/problemnew/show/SP1805 http://poj.org/problem?id=2559 思路: ## 单调栈 首先如果所有矩 ...

  9. Bloxorz I POJ - 3322(广度优先搜索)

    题意:传送门 对应游戏:传送门 题解:对于这个木块,可以发现有三个状态,第一个状态是直立的,第二个状态是横躺着,第三个状态是竖躺着,那么状态维护三个值x,y,liex,y,liex,y,lie,分别对 ...

最新文章

  1. 生产系统支撑终端故障处理的三个误区
  2. 解决Vue打包后背景图片路径错误问题
  3. 牛客 牛牛做除法II
  4. Matlab一维数据的中值滤波与均值滤波函数
  5. ADNI数据集阅读整理
  6. 改进YOLOv7系列:24.添加SimAM注意力机制
  7. Python学习随笔:PyCharm的错误检测使用及调整配置减少错误数量
  8. 知名油漆涂料品牌排行榜前十名
  9. Unity中的宏定义
  10. GE Proficy多个漏洞
  11. 一网打尽OkHttp中的缓存问题
  12. 内存颗粒和闪存颗粒的区别_闪存颗粒到底是何物?浅析闪存及制程
  13. 机器人或将人类推向“无能之下的自由”
  14. C/C++万能头文件
  15. Android MVP架构模式
  16. windows10 critical_service_failed解决办法
  17. 为什么只有好超市,才敢卖熟牛油果?
  18. 如何进行学习——结构化思维
  19. 5e怎么绑定一键跳投_企业邮箱如何绑定微信?微信怎么添加邮箱?
  20. spad dtof lidar车载IMX459更新系列一特性和功能

热门文章

  1. 【漏洞验证】NFS 共享信息泄露漏洞
  2. Dell R710 iDRAC6 远程控制卡设置
  3. 解析jsonarra_解析Json——操纵JsonArray
  4. 计算机专业简历英文简历,计算机专业英文简历范文
  5. 世界级标杆项目!联合利华全国首个全品类生产和营销基地落户广州;菲仕兰中国首家体验店在上海黄浦正式启用 | 美通社头条...
  6. mysql Copying to tmp table时间占用过长的终极解决方法
  7. 浏览器表标准符合性测试实践
  8. hive中的逻辑运算符 案列_Hive 教程(官方Tutorial)
  9. 【C语言】通讯录设计
  10. 锤子科技官网:问题整理及注意事项