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;
}