题意:一群蚂蚁走在一条长度为L 公分的绳子上,每只蚂蚁的速度为1 cm/sec。当一只蚂蚁走到绳子的尽头时,它马上掉下绳子(再也爬不起来了)。当两只蚂蚁在绳子上相遇时,马上掉头往另一个方向走去。我们知道每只蚂蚁在绳子上的位置,但不幸的是,我们并不知道每只蚂蚁开始时走的方向。你的任务是算出最快和最慢可能需要多少时间,所有的蚂蚁都掉出绳子外。
方法:经典的蚂蚁问题,最短的时间是两边的蚂蚁分别往两边走的情况,时间为离中间最近的蚂蚁到两边的距离。最长时间为蚂蚁距离远的那一端距离的最大值,只考虑两头的蚂蚁就行。蚂蚁相撞等于对穿而过。
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <ctime> using namespace std; const int maxn = 1000000+10; int pos[maxn]; int main() { #ifdef Local freopen("a.in", "r", stdin); #endif int t = 0; cin >> t; for(int kase = 1; kase <= t; kase++) { int p = 0; memset(pos, 0, sizeof(pos)); int L = 0, n = 0, i = 0, j = 0, p1 = 0, p2 = 0; cin >> L >> n; for (i = 0; i < n; i++) cin >> pos[i]; double mid = L/2.0, min = 1 << 30, max = 0; for (i = 0; i < n; i++) { double temp = fabs(pos[i] - mid); if (temp < min) { min = temp; p1 = pos[i];} } if (p1 <= mid) cout << p1 << ‘ ‘; else cout << L-p1 << ‘ ‘; sort(pos, pos+n); if (pos[0] <= mid && pos[n-1] > mid) { if (L-pos[0] >= pos[n-1]) p2 = L - pos[0]; else p2 = pos[n-1]; } else if (pos[0] <= mid && pos[n-1] <= mid) p2 = L - pos[0]; else p2 = pos[n-1]; cout << p2 << endl; } }
1、改成100没改回去导致RE。2、大数组定义在外边。
uva - 10714 - Ants(数学推导、蚂蚁问题),布布扣,bubuko.com
原文:http://blog.csdn.net/u013545222/article/details/19963445