zoukankan      html  css  js  c++  java
  • poj1852 Ants

    说一个长度为m的杆,上面有n个蚂蚁,告诉每个蚂蚁的初始位置,每个蚂蚁速度都是一样的,问所有的蚂蚁离开杆的最短和最长时间是多少。

    模拟题,所有的蚂蚁看成一样的,可以这样理解,即使相撞按反方向走,但是两只蚂蚁从开始爬到相撞到继续相背爬,这个时间都是一样的。所以直接对每只蚂蚁分别处理,得出他的最短离开时间和最长离开时间,我们分别从最短离开时间和最长离开时间里面求出最大的。

    源码:

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int m,n,l,pos,left,right,curMin,curMax,rsMin,rsMax;
        scanf("%d",&m);
        while(m-->0)
        {
            scanf("%d%d",&l,&n);
            rsMin=-1;
            rsMax=-1;
            for(int i=0;i<n;i++)
            {
                scanf("%d",&pos);
                left=pos;
                right=l-pos;
                curMin=left<=right?left:right;
                curMax=(curMin==left)?right:left;
                if(curMin>rsMin)
                    rsMin=curMin;
                if(curMax>rsMax)
                    rsMax=curMax;
            }
            printf("%d %d\n",rsMin,rsMax);
        }
        return 0;
    }

  • 相关阅读:
    SQL Server 2005 中的同义词
    SQL SERVER 2005中同义词实例
    聚集索引和非聚集索引(整理)
    linux kernel中timer的使用
    linux命令: patch
    win7(64位)php5.5-Apache2.4-mysql5.6环境安装
    tasklet和工作队列
    linux串口编程(c)
    Notepad++中Windows,Unix,Mac三种格式
    centos7/redhat7 将网卡名字改成eth样式的方法
  • 原文地址:https://www.cnblogs.com/buptLizer/p/2168063.html
Copyright © 2011-2022 走看看