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

    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

     
    解题思路:这道题说两个蚂蚁相遇时,会往反向走。就相当于两只蚂蚁相遇,不往反向走(即不断向前走)。所以最短的用时相当于距离极点最远的那只蚂蚁走路需要的时间;最长用时相当于距离某一段特别远的那只蚂蚁的用时。
     
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    #define ll long long
    
    const int inf = 1e6+8;
    
    int n, position[inf], len, number, s[inf], r[inf];
    int main()
    {
        scanf("%d", &n);
        for(int i = 0; i<n; i++)
        {
            int little, big, miao = 0, root = 0;
            scanf("%d%d", &len, &number);
            for(int j = 0; j<number; j++)
            {
                scanf("%d", &position[j]);
                s[j] = min(position[j], len-position[j]);
                r[j] = max(position[j], len-position[j]);
            }
            sort(s, s+number, greater<int>());
            sort(r, r+number, greater<int>());
            little = s[0];
            big = r[0];
            printf("%d %d
    ", little, big);
        }
        return 0;
    }
  • 相关阅读:
    ZOJ Problem Set–2417 Lowest Bit
    ZOJ Problem Set–1402 Magnificent Meatballs
    ZOJ Problem Set–1292 Integer Inquiry
    ZOJ Problem Set–1109 Language of FatMouse
    ZOJ Problem Set–1295 Reverse Text
    ZOJ Problem Set–1712 Skew Binary
    ZOJ Problem Set–1151 Word Reversal
    ZOJ Problem Set–1494 Climbing Worm
    ZOJ Problem Set–1251 Box of Bricks
    ZOJ Problem Set–1205 Martian Addition
  • 原文地址:https://www.cnblogs.com/RootVount/p/10440093.html
Copyright © 2011-2022 走看看