zoukankan      html  css  js  c++  java
  • [POJ1852]Ants

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 12431   Accepted: 5462

    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
    

    Source

    Thinking

      如果这个题使用穷竭搜索的话,时间复杂度O(2^n);显然不行,我们可以采取一种更为巧妙的方法。如果两只蚂蚁碰头以后,他们互相折返,能不能把他们看成是向左的蚂蚁按照的向右的方向向右走,向右的亦是如此,实质上就是两个蚂蚁继续按照原方向行进。所以我们维护一个最小值,一个最大值即可。

    var n,m,i,j,mint,maxt,t:longint;
        a:array[1..100000] of longint;
    function min(x,y:longint):longint;
    begin
        if x>y then exit(y) else exit(x);
    end;
    function max(x,y:longint):longint;
    begin
        if x>y then exit(x) else exit(y);
    end;
    procedure main;
    var i:longint;
    begin
        readln(m,n);
        for i:=1 to n do
            read(a[i]);
        mint:=0;
        maxt:=0;
        for i:=1 to n do
            begin
                mint:=max(mint,min(a[i],m-a[i]));
                maxt:=max(maxt,max(a[i],m-a[i]));
            end;
        writeln(mint,' ',maxt);
    end;
    begin
        readln(t);
        for i:=1 to t do main;
    end.
    View Code
  • 相关阅读:
    深度学习丨Deep Learning学习资源整理
    机器学习丨《机器学习》、《统计学习方法》思维导图
    概率统计丨陈希孺《概率论与数理统计》思维导图
    数据科学丨DataScience工具速查卡
    线性代数丨《线性代数及其应用》思维导图
    Api接口加密策略
    tomcat重启警告:Abandoned connection cleanup thread 服务器宕机解决方案
    spring mvc多环境下配置文件的设置
    mysql中将查询结果进行拼接处理及concat、group_concat的使用
    JVM优化之 -Xss -Xms -Xmx -Xmn 参数设置
  • 原文地址:https://www.cnblogs.com/yangqingli/p/4883668.html
Copyright © 2011-2022 走看看