ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 1852 Ants

2019-02-26 21:02:00  阅读:372  来源: 互联网

标签:1852 number ants two pole POJ Ants each include


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

Waterloo local 2004.09.19   解题思路:这道题说两个蚂蚁相遇时,会往反向走。就相当于两只蚂蚁相遇,不往反向走(即不断向前走)。所以最短的用时相当于距离极点最远的那只蚂蚁走路需要的时间;最长用时相当于距离某一段特别远的那只蚂蚁的用时。  
#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\n", little, big);
    }
    return 0;
}

 

标签:1852,number,ants,two,pole,POJ,Ants,each,include
来源: https://www.cnblogs.com/RootVount/p/10440093.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有