Description:
2 10 3 2 6 7 214 7 11 12 7 13 176 23 191Sample 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;
}