题意: 在x坐标系上有一些蚂蚁, 每次移动一格, 方向由你来指定, 求出所有蚂蚁(即最后一个蚂蚁)下落的最短和最长时间.
这是白书上的一道贪心例题, 遇见这种题不要慌, 先分类讨论一下, 仔细思考每个状态的性质, 无外乎以下三种.
1.始终没有碰撞, 直接向最近的一端移动
2.发生碰撞, 然后反向移动
仔细思考, 不存在多次碰撞(因为速度相同), 而碰撞的话其实对两只蚂蚁的路径长度没有任何影响(视为交换即可), 由此可以推得, 最快的方案不过就是直接走向最近的一端
按照题意枚举即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const LL maxn = 1000010;
int T, l, n, x[maxn];
int main()
{
cin >> T;
while(T--){
scanf("%d %d",&l,&n);
ms(x, 0);
for(int i = 1; i <= n; i++)
scanf("%d",&x[i]);
int minT = 0;
//所有蚂蚁下落的最短时间
for(int i = 1; i <= n; i++)
minT = max(minT, min(x[i], l-x[i]));
int maxT = 0;
for(int i = 1; i <= n; i++)
maxT = max(maxT, max(x[i], l-x[i]));
printf("%d %d\n",minT, maxT);
}
return 0;
}