题目:

http://poj.org/problem?id=1852

题意:

长为L的杆子上有n只蚂蚁在爬行,当蚂蚁爬到杆子的端点时就会掉下去,两只蚂蚁相遇不能穿行而过,而是两者都掉头回去接着爬。问所有蚂蚁都掉下去的最短时间和最长时间

思路:

看的挑战程序设计。首先最短时间时,不会出现蚂蚁相遇,蚂蚁会朝离其较近的端点爬,这里面的最大值就是答案。最长时间时,两只蚂蚁相遇时,它们的状态是一样的,可以认为它们穿行而过,这不会对结果造成影响,想一下就明白了,因为是最长时间,蚂蚁都朝距离较远的端点爬,这里面的最大值就是答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 1000010;
const int INF = 0x3f3f3f3f;
int main()
{
    int t, n, m, a;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        int res1 = -INF, res2 = -INF;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d", &a);
            res1 = max(res1, min(a, n - a));
            res2 = max(res2, max(a, n - a));
        }
        printf("%d %d\n", res1, res2);
    }
    return 0;
}