POJ 1852 Ants

我们两清 提交于 2020-02-02 00:02:56

 

 题解:想象两只蚂蚁相遇后朝反方向走,如果无视不同蚂蚁的区别,可以认为是保持原样交错通过继续前进不会有任何问题,可

以认为蚂蚁是独立运动,求最长时间就是求蚂蚁到杆子端点的最大距离,求最短时间就是求蚂蚁到杆子端点的最短距离。

时间复杂度 O( n )

#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e6 + 7;
int num[N];
int L,n;
int t;
void solve()
{
    int minT = 0, maxT = 0;
    for(int i = 0; i < n; i++) minT = max(minT,min(num[i],L - num[i])); //找最小值
    for(int i = 0; i < n; i++) maxT = max(maxT,max(num[i],L - num[i]));  //找最大值
    printf("%d %d\n",minT,maxT);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(num,0,sizeof(num));
        scanf("%d %d",&L,&n);
        for(int i = 0; i < n; i++) scanf("%d",&num[i]);
        solve();
    }
    return 0;
}

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!