• 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
    ", a1, a2);
        }
        return 0;
    }
    
  • 相关阅读:
    线段树的区间更新---A Simple Problem with Integers
    划分树---Feed the dogs
    划分树基础知识
    Treap树的基础知识
    平衡二叉树---Shaolin
    KMA & ex_KMP---Revolving Digits
    DP---Mahjong tree
    《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量
    《程序员代码面试指南》第一章 栈和队列 求最大子矩阵大小
    《程序员代码面试指南》第一章 栈和队列 构造数组的MaxTree
  • 原文地址:https://www.cnblogs.com/Z8875/p/12700165.html
Copyright © 2020-2023  润新知