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;
    }
    
  • 相关阅读:
    例子:动能并不是特别强(2-3)后,下M5的同时,也是恢复期到期的前一天
    .NET 自带的动态代理+Expression 实现AOP
    自旋锁-SpinLock(.NET 4.0+)
    使用Nito.AsyncEx实现异步锁
    C# 两行代码实现 延迟加载的单例模式(线程安全)
    C++ 用于大型程序的工具
    C++ 模板与泛型编程
    C++ 面向对象编程
    C++ 重载操作符与转换
    C++ 复制控制
  • 原文地址:https://www.cnblogs.com/shawk/p/12700165.html
Copyright © 2011-2022 走看看