uva 10714

来源:互联网 发布:tp886 访客网络 编辑:程序博客网 时间:2024/05/29 18:54

给你几只蚂蚁的位置

求所有蚂蚁掉下去的最短时间和最长时间

蚂蚁相遇之后会转向

等价于他们向前走

只是编号换一下


先排个序

最短时间就是蚂蚁不相遇

直接朝着两端走

取决于中间的的那只蚂蚁


最长时间就是相遇

取决于离某一端距离最长的蚂蚁


#include <cstdio>#include <cstdlib>#include <algorithm>#define N 1000010using namespace std;int pos[N];int main(){int cas, length, n;scanf("%d", &cas);while (cas--) {scanf("%d%d", &length, &n);for (int i = 0; i < n; i++)scanf("%d", &pos[i]);sort(pos, pos + n);int ans1 = 0, ans2 = 0;for (int i = 0; i < n; i++) {int temp = min(pos[i], length - pos[i]);if (temp > ans1)ans1 = temp;temp = max(pos[i], length - pos[i]);if (temp > ans2)ans2 = temp;}printf("%d %d\n", ans1, ans2);}return 0;}


0 0