POJ 1852 Ants(思维)

Ants

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 24676   Accepted: 9850

Description

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

Input

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

Output

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time. 

Sample Input

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output

4 8
38 207

题意:每只蚂蚁的速度为1cm/s, 有一根杆子,杆子的长度为L cm, 蚂蚁相遇的时候会各自反向爬回去,当蚂蚁到达端点的时候会掉下去,现给出 n 只蚂蚁与左端点的距离 (初始朝向未知),问所以蚂蚁掉下去的最短时间与最长时间。

扫描二维码关注公众号,回复: 3476332 查看本文章

思路:这是个思维题,最短时间很好想,就是每一只蚂蚁的朝向都为近的端点。现在考虑最长时间的情况,考虑两只蚂蚁相遇, 假设蚂蚁 A 朝向为右,蚂蚁B 朝向为左,当两只蚂蚁相遇的时候,蚂蚁B 朝右,蚂蚁A 朝左,但是可以理解为,蚂蚁A ,B交换了身体,还是蚂蚁A 朝右,蚂蚁B 朝左。可以画图理解理解,很好理解的。 那么最长的时间就变成了   每只蚂蚁朝向为远的端点。

AC代码:

#include<cstdio>
#include<algorithm>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

const int maxn = 1e6 + 10;
int x[maxn];

int main()
{
    int t; scanf("%d",&t);
    while(t--){
        INT(L); INT(n);
        rep(i,0,n) scanf("%d",&x[i]);
        int minn = 0,maxx = 0;
        rep(i,0,n){
            int a = min(x[i],L - x[i]);
            int b = max(x[i],L - x[i]);
            minn = max(a,minn);
            maxx = max(maxx,b);
        }
        printf("%d %d\n",minn,maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82459622