zoukankan      html  css  js  c++  java
  • poj 1852 Ants

    Ants
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 14257   Accepted: 6216

    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

    首先很容易想到一个穷竭搜索算法,即枚举所有蚂蚁的初始朝向的组合,这可以利用递归函数实现.
    每只蚂蚁的初始朝向都有2种可能,n只蚂蚁就是2×2×…×2=2n种。如果n比较小,这个算法还是可行的,但指数函数随着n的增长会急剧增长。2n增长的趋势
    n 1 5 10 20 30 100 10000 1000000
    2n 2 32 1024 1048576 109 1030 103010 10301030
    穷竭搜索的运行时间也随之急剧增长。一般把指数阶的运行时间叫做指数时间。指数时间的算法无法处理稍大规模的输入。
    接下来,让我们来考虑比穷竭搜索更高效的算法。首先对于最短时间,看起来所有蚂蚁都朝向较近的端点走会比较好。事实上,这种情况下不会发生两只蚂蚁相遇的情况,而且也不可能在比此
    更短的时间内走到竿子的端点。
    接下来,为了思考最长时间的情况,让我们看看蚂蚁相遇时会发生什么。
    事实上,可以知道两只蚂蚁相遇后,当它们保持原样交错而过继续前进也不会有任何问题。这样看来,可以认为每只蚂蚁都是独立运动的,所以要求最长时间,只要求蚂蚁到竿子端点的最大距
    离就好了。这样,不论最长时间还是最短时间,都只要对每只蚂蚁检查一次就好了,这是O(n)时间的算法。对于限制条件n ≤ 106,这个算法是够用的,于是问题得解。
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 int main(){
     6     int test, m, n;
     7     cin >> test;
     8     while(test--){
     9         int least = 0, most = 0;
    10         cin >> m >> n;
    11         int x;
    12         for(int i = 0; i < n; i++){
    13             cin >> x;
    14             least = max(least, min(x, m - x));
    15             most = max(most, max(x, m - x));
    16         }
    17         printf("%d %d
    ", least, most);
    18     }
    19     return 0;
    20 } 
    
    
    
    
    
  • 相关阅读:
    kafka 流式计算
    解决山地车令人讨厌的中轴异响及其他异响问题
    go语言通道详解
    使用Spring Cloud连接不同服务
    并发之痛 Thread,Goroutine,Actor
    用go语言实现线程池
    golang go语言通道类型的通道示例 通道的通道
    Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
    spring5 reactive
    Go 语言和 Scala 语言对比
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5564519.html
Copyright © 2011-2022 走看看