01 题目:POJ 1852 Ants

题目:http://poj.org/problem?id=1852

Ants
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19204 Accepted: 8024
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只蚂蚁以1cm/s的速度在长L的竿子上爬行,在爬到竿子的一端的时候就会掉下去,同时当两只蚂蚁相遇时他们不能交错同行,只能各自往返,对于每只蚂蚁呢,我们知道了它距离竿子左端的位置xi,但不知道朝向,问所有蚂蚁落下的最短时间和最长时间。

思路:
  首先最简单的是暴力,但是数据量是1e6那就是说暴力必定会导致TLE,那我们可以先考虑一下两只蚂蚁相遇的时候会怎么样,最短的就是两者同时朝着离端点最短的方向走,那就是xi与L-xi中取min,那最长的情况就是两者要相遇,那相遇之后呢?相遇之后又各自往回走,如果按照这样的方式计算呢,跟之前的暴力是一样的,如果我们不看两只蚂蚁的区别只看他们走的路线你就会发现——他们所走的路线就是可以交叉通过之后的长度,那么对于每一个蚂蚁就是找出离端点的最长和最短时间就行了,然后在最短时间和最长时间里面找到最大值。是时间复杂度为O(n)的算法。

代码:

 /*
挑战程序设计竞赛题解库
题目:http://poj.org/problem?id=1852 Ants
Created by TyxMaek1997-2017
*/

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

#define MAXN 1000009
//#define Debug //提交时注释此行仅供本地数据测试时使用

int main()
{
#ifdef Debug
    freopen("in.txt", "r", stdin);
#endif
    int n, l, m;
    scanf("%d", &n);
    while (n--)
    {
        int maxt = 0, mint = 0;
        int xi = 0;
        scanf("%d%d", &l, &m);
        while (m--)
        {
            scanf("%d", &xi);
            maxt = max(maxt, max(xi, l - xi));//求最长时间
            mint = max(mint, min(xi, l - xi));//求最短时间
        }
        printf("%d %d\n", mint, maxt);
    }
    return 0;
}
/*-----------------©TyxMaek1997-2017--------------------*/
内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值