POJ 1852 Ants

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define sc(x) scanf("%d",&(x))
 6 #define sc2(x,y) scanf("%d%d", &(x), &(y))
 7 #define pf(x) printf("%d\n", x)
 8 #define pf2(x, y) printf("%d %d\n", x, y)
 9 #define FOR(i,b,e) for(i=b; i<=e; i++)
10 #define CL(x,y) memset(x,y,sizeof(x))
11 const int MAX = 1000000;
12 int ant[MAX], Min[MAX];
13 using namespace std;
14 int main(int argc, char *argv[]) {
15     int T, L, n, i, k, max;
16     sc(T);
17     while(T--){
18         max = 0; k = 0;
19         CL(Min, 0);
20         sc2(L, n);
21         FOR(i, 0, n-1) sc(ant[i]);
22         sort(ant, ant+n);
23         max = ant[n-1] > (L-ant[0]) ? ant[n-1] : (L-ant[0]);
24         FOR(i, 0, n-1){
25             if(ant[i] <= L/2){
26                 Min[k++] = ant[i];
27             }
28             else{
29                 Min[k++] = L- ant[i];
30             }
31         }
32         sort(Min, Min+k);
33         pf2(Min[k-1], max); 
34     }    
35     return 0;
36 }
View Code
/*
1.最快:找出最靠近中间的点,它走到离它最近的端点的时间就是全部蚂蚁掉下去的时间

2.最慢:对于每个点,找出它到两个端点的最远距离就是全部蚂蚁掉下去的时间
*/
posted @ 2015-04-01 14:36  PastLIFE  阅读(112)  评论(0编辑  收藏  举报