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

题目大意:

一根水平的棍子上有n只蚂蚁, 每只蚂蚁在离棍子左端x m远以1m/s的速度以任意方向在棍子上爬行, 若两只蚂蚁相遇则会掉头, 问所有蚂蚁掉落的最长最短时间分别是多少。

解题思路:

首先我们并不知道蚂蚁的初始方向是朝着哪边爬行, 所以我们把所有蚂蚁离散化(其实就是把一只蚂蚁看成所有蚂蚁), 那么问题就变成了求一只蚂蚁掉落的最长时间和最短时间。 因为棍子有两端, 所以我们要计算出蚂蚁距离棍子两端的距离。 对于最短时间, 我们取每只蚂蚁离棍子较短的那个值, 因为要保证所有蚂蚁都掉落, 所以我们在所有蚂蚁的最小时间里取最大值, 同样对于最长时间我们则是取所有蚂蚁对于棍子最长距离里取最大值。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*tools:
 *ios::sync_with_stdio(false);
 *freopen("input.txt", "r", stdin);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6 + 7;

int n, m, num;

int main() {
    //freopen("input.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--) {
        int minx = -1, maxx = -1;
        scanf("%d %d", &n ,&m);
        for (int i = 0; i < m; ++i) {
            scanf("%d", &num);
            minx = max(min(num, n - num), minx);
            maxx = max(max(num, n - num), maxx);
        }
        printf("%d %d\n", minx, maxx);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章