zoukankan      html  css  js  c++  java
  • 51nod 1266 蚂蚁

    题目来源: Poj
    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:2级算法题
     收藏
     关注
    n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行。当蚂蚁爬到竿子的端点时就会掉落。由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反向爬回去。对于每只蚂蚁,我们知道它距离竿子左端的距离xi,但不知道它当前的朝向。请计算各种情况当中,所有蚂蚁落下竿子所需的最短时间和最长时间。
     
     

    例如:竿子长10cm,3只蚂蚁位置为2 6 7,最短需要4秒(左、右、右),最长需要8秒(右、右、右)。
    Input
    第1行:2个整数N和L,N为蚂蚁的数量,L为杆子的长度(1 <= L <= 10^9, 1 <= N <= 50000)
    第2 - N + 1行:每行一个整数A[i],表示蚂蚁的位置(0 < A[i] < L)
    Output
    输出2个数,中间用空格分隔,分别表示最短时间和最长时间。
    Input示例
    3 10
    2
    6
    7
    Output示例
    4 8



    方法是将线上的点以中点分割成左部和右部,求shrt求左部的坐标最大值,求右部的(l-坐标)最大值;求lon求左部(l-坐标)最大值,求右部坐标最大值。

    上代码:


    #include<iostream>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    int ma[50005];
    const int maxn=1e9+10;
    int main()
    {
        int n,l;
        cin>>n>>l;
        for(int i=0;i<n;i++)
            cin>>ma[i];
        sort(ma,ma+n);
        int shrt=0,lon=0;
        int middle=l/2;
        for(int i=0;i<n;i++)
        {
            if(ma[i]<middle)
            {
                shrt=max(ma[i],shrt);
                lon=max(l-ma[i],lon);
            }
            else
            {
                shrt=max(l-ma[i],shrt);
                lon=max(ma[i],lon);
            }
        }
        cout<<shrt<<" "<<lon<<endl;
    
    
        return 0;
    }
    










  • 相关阅读:
    leetcode-409
    leetcode-836
    leetcode-1160
    leetcode-面试题13
    leetcode-695
    go的一些小lib
    leetcode-300
    cookie
    php上传文件
    PHP 文件创建/写入
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387014.html
Copyright © 2011-2022 走看看