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

    Ants
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 6418   Accepted: 3010

    Description

    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

    这题怎么归类呢……一道在《挑战程序设计竞赛》上看到的题吧

    题目中说两只蚂蚁相遇后只能各自调头向反方向走,不能交错而过……介是稍微想一下就知道无论是调头还是交错而过都是一样的

    这样问题就好解决了,我们只要把所有蚂蚁都按交错而过来计算时间就可以了

     

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 long len,ants;
     7 long Left[1000010],Right[1000010];
     8 
     9 long Max(long a,long b)
    10 {
    11     return a>b?a:b;
    12 }
    13 
    14 long Min(long a,long b)
    15 {
    16     return a<b?a:b;
    17 }
    18 
    19 int main()
    20 {
    21     int t;
    22 
    23     scanf("%d",&t);
    24 
    25     while(t--)
    26     {
    27         long maxn,minn;
    28 
    29         scanf("%ld %ld",&len,&ants);
    30 
    31         for(int i=1;i<=ants;i++)
    32         {
    33             scanf("%ld",&Left[i]);
    34             Right[i]=len-Left[i];
    35         }
    36 
    37         minn=Min(Left[1],Right[1]);
    38         for(int i=2;i<=ants;i++)
    39             minn=Max(minn,Min(Left[i],Right[i]));
    40 
    41         maxn=Max(Left[1],Right[1]);
    42         for(int i=2;i<=ants;i++)
    43             maxn=Max(maxn,Max(Left[i],Right[i]));
    44 
    45         printf("%ld %ld
    ",minn,maxn);
    46     }
    47 
    48     return 0;
    49 }
    [C++]
  • 相关阅读:
    最大最小值得判断代码
    等腰三角形的代码及各类代码
    Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别
    Java 集合类的特性
    Java 用程序给出随便大小的10 个数,序号为1-10,按从小到大顺序输出,并输出相应的序号?
    List、Map、Set三个接口,存取元素时,各有什么特点?
    Java 清除数组相同元素
    eclipse导入项目出现叹号处理方法:
    初学者-PHP笔记
    java 对象输入输出流
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3219948.html
Copyright © 2011-2022 走看看