zoukankan      html  css  js  c++  java
  • Ants(POJ 1852)

    Ants
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 24129   Accepted: 9652

    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
    • 题解:
      1. 最短时间很简单,所有蚂蚁往较近的一端走即可,这种情况下也不会发生两只蚂蚁相遇的情况,最短时间就是每只蚂蚁所走最短时间的最大值
      2. 对于最长时间,我们需要考虑两只蚂蚁相遇的情况,而事实上,两只蚂蚁相遇之后各自反向前进与两只蚂蚁相遇之后保持原样交错而过继续前进是等效的,因此可认为每只蚂蚁都是独立运动的,最长时间就是每只蚂蚁往较长一端走的时间的最大值
    • 代码:
       1 #include <iostream>
       2 
       3 using namespace std;
       4 
       5 template <typename T>
       6 T minn(T, T);
       7 
       8 template int minn<int>(int, int);
       9 
      10 template <typename T>
      11 T maxn(T, T);
      12 
      13 template<> int maxn<int>(int, int);
      14 
      15 int main(int argc, char * argv[])
      16 {
      17     int k;
      18     for (cin >> k; k>0; k--)
      19     {
      20         int l, n;
      21         cin >> l >> n;
      22         int * a = new int[n];
      23         for (int i=0; i<n; i++) cin >> a[i];
      24         int tmin=0, tmax=0;
      25         for (int i=0; i<n; i++) 
      26         {
      27             tmin=maxn(tmin,minn(a[i],l-a[i]));
      28             tmax=maxn(tmax,maxn(a[i],l-a[i]));
      29         }
      30         cout << tmin << ' ' << tmax << endl;
      31     }
      32     return 0;
      33 }
      34 
      35 template <typename T>
      36 T minn(T x, T y)
      37 {
      38     if (x<y) return x;
      39     return y;
      40 }
      41 
      42 template <typename T>
      43 T maxn(T x, T y)
      44 {
      45     if (x>y) return x;
      46     return y;
      47 }
      48 
      49 template<> int maxn<int>(int x, int y)
      50 {
      51     if (x>y) return x;
      52     return y;
      53 }
  • 相关阅读:
    margin折叠及hasLayout && Block Formatting Contexts
    视口(viewport)原理详解之第二部分(移动端浏览器)
    Git & SSH
    .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法
    阿里云函数计算 .NET Core 初体验
    Oracle PLSQL 存储过程无法进入单步调试
    解释器错误:没有那个文件或目录
    不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。
    Oracle 11G R2 安装图解
    Oracle UTL_HTTP
  • 原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9404109.html
Copyright © 2011-2022 走看看