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

    Ants

    Time Limit: 1000ms
    Memory Limit: 30000KB
    This problem will be judged on PKU. Original ID: 1852
    64-bit integer IO format: %lld      Java class name: Main
    An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
     

    Input

    The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
     

    Output

    For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time. 
     

    Sample Input

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

    Sample Output

    4 8
    38 207
    

    Source

     
    解题:因为相遇会转向,看成不转向,那么不转向,就是直接穿过去了。既然无蚁可挡,那么直接就走上端点掉下去了。。。
     
    最短时间所有蚂蚁都掉下去。。。最长时间所有蚂蚁掉下去
     
     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 const int INF = 0x3f3f3f3f;
     5 int main(){
     6     int n,m,theMin,theMax,T,cur;
     7     scanf("%d",&T);
     8     while(T--){
     9         scanf("%d %d",&n,&m);
    10         theMin = theMax = 0;
    11         for(int i = 0; i < m; ++i){
    12             scanf("%d",&cur);
    13             theMin = max(theMin,min(n-cur,cur));
    14             theMax = max(theMax,max(n-cur,cur));
    15         }
    16         printf("%d %d
    ",theMin,theMax);
    17     }
    18     return 0;
    19 }
    View Code
  • 相关阅读:
    Linux初级知识_04 -- 目录结构与目录管理
    查找无限整数序列的第n位1,2,3,4,5,6,7,8,9,10,11,...
    谷歌面试题:在半径为1的圆中随机选取一点
    FtpClient 调用storeFile 抛出 java.net.SocketException异常
    CountDownLatch 使用
    软件版本号比较 java工具类
    pcm文件转wav C语言
    unimrcp更改安装目录
    xpath学习记录
    jackson 实体转json 为NULL或者为空不参加序列化
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4552964.html
Copyright © 2011-2022 走看看