POJ 1852 Ants(贪心)

POJ 1852 Ants

题目大意

有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最短时间和最长时间。

Input

输入的第一行包含一个整数,给出随后的个案数。每种情况的数据均以两个整数开头:极点的长度(以厘米为单位)和n(极点上的蚂蚁数)。这两个数字后面紧跟着n个整数,给出了每个蚂蚁在杆上的位置,以从杆左端开始的距离(无特定顺序)。所有输入整数均不大于1000000,并且它们之间用空格分隔。

Output

对于每种输入情况,输出两个数字,并用一个空格隔开。第一个数字是所有蚂蚁掉下来的最早可能的时间(如果正确选择了它们的行走方向),第二个数字是这样的最新可能的时间。

Sample Input

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

Sample Output

4 8
38 207

Solution

水题
像极了小学奥数的相遇问题
两者总路程为2n
那么一样的
蚂蚁相遇后不掉头和掉头的总路程不变
直接贪心让每只蚂蚁走离自己最近的端点即可

暴力贪心就完了……

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
// #define int long long
using namespace std;

inline int read(){
    int x = 0, w = 1;
    char ch = getchar();
    for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') w = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    return x * w;
}

int main(){
    int T = read();
    int maxv = 0, minv = 0;
    while(T--){
        int l = read(), n = read();
        maxv = 0, minv = 0;
        int tmp = 0;
        for(int i = 1; i <= n; i++){
            tmp = read();
            maxv = max(maxv, max(tmp, l - tmp));
            minv = max(minv, min(tmp, l - tmp));
        }
        cout << minv << " " << maxv << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/rui-4825/p/12700065.html