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;
    }
  • 相关阅读:
    屏幕的真实分辨率大小
    CCConfiguration::sharedConfiguration()->loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案
    git 放弃提交到提交之前
    cocos2d-x 输出debug信息
    Ubuntu设置环境变量
    有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息.
    读取 android sys/下的信息
    android 读取 raw 中的文件。
    C/C++中结构体(struct)
    异步图片下载引擎(升级版——ExecutorService+handler)
  • 原文地址:https://www.cnblogs.com/RootVount/p/10440093.html
Copyright © 2011-2022 走看看