zoukankan      html  css  js  c++  java
  • R Ants思维题

    Ants

    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
    

    题解

    题目大意

    T组测试数据, 每组两行
    第一行两个数,分别是棍子长度和蚂蚁数量
    第二行是每只蚂蚁离左端的距离
    每只蚂蚁单位时间走1单位长度
    两只蚂蚁相遇后都会掉头
    问所有蚂蚁都从棍子上下来所需的最短时间和最长时间

    解题思路

    如果题目没有说两只蚂蚁相遇后都会掉头的话
    那么就很好想
    我看到这个条件也是很懵逼
    相遇后就掉头,没这个条件就是直接穿过
    那有啥区别呢??
    仔细一想,没有区别
    是的没有区别,两只蚂蚁是无法区分的
    那掉头和直接穿过是等价的
    就是怎么简单

    代码

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    int m, n, T;
    int main() {
        scanf("%d", &T);
        while (T--) {
            scanf("%d%d", &m, &n);
            int a1 = 0, a2 = 0; 
            for(int i = 1, x; i <= n; i++) {
                scanf("%d", &x);
                a1 = max(a1, min(x, m-x));
                //时间最小需要向近的那一边走
                a2 = max(a2, max(x, m-x));
                //时间最大需要向远的那一边走
            }
            printf("%d %d\n", a1, a2);
        }
        return 0;
    }
    
  • 相关阅读:
    Maven与Eclipse整合使用
    Maven学习总结(五)——聚合与继承【如果想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合】
    Maven的核心概念:maven仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的
    使用Maven构建项目---【重点需要掌握:Jetty和Maven两种项目骨架】
    Maven项目构建过程练习
    maven编码gbk的不可映射字符”解决办法
    Maven
    Spring Aop的方法执行简单模型
    2016年看过的书
    ExcelReader
  • 原文地址:https://www.cnblogs.com/shawk/p/12700165.html
Copyright © 2011-2022 走看看