zoukankan      html  css  js  c++  java
  • POJ 1852 Ants

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

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

    时间复杂度 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
    ",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;
    }
    

      

  • 相关阅读:
    第二阶段站立会议03
    第二阶段站立会议02
    第二阶段站立会议01
    第十一周进度条
    小强大扫荡
    测试计划
    用户体验
    各组意见
    第一阶段绩效评估
    站立会议10
  • 原文地址:https://www.cnblogs.com/Edviv/p/12250523.html
Copyright © 2011-2022 走看看