Ants
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 10735 | Accepted: 4762 |
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
首先时间最短的情况 那就是每只蚂蚁各自向最近的端点走就可以了 由于每个蚂蚁位置不用 不可能会相遇
时间最长的情况 各自向最远的端点走 会发生蚂蚁相遇 如果两只蚂蚁相遇了 各自反向走回去 然而事实上 即使交叉走过 也没有问题 因为最终的结果都是两只蚂蚁向另一个方向走了
然后枚举每一只蚂蚁就可以了
AC代码如下:
//
// POJ 1852 Ants
//
// Created by TaoSama on 2015-02-16
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; cin >> t;
while(t--) {
int l, n, x; cin >> l >> n;
int MinT = 0, MaxT = 0;
for(int i = 1; i <= n; ++i) {
cin >> x;
MinT = max(MinT, min(x, l - x));
MaxT = max(MaxT, max(x, l - x));
}
cout << MinT << ' ' << MaxT << endl;
}
return 0;
}