POJ1852 UVa10714 ZOJ2376 Ants【水题】

509 篇文章 6 订阅
232 篇文章 0 订阅

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 18360 Accepted: 7736

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

Source



问题链接POJ1852 UVa10714 ZOJ2376 Ants

问题简述

  很多的蚂蚁都在长度为l(cm)的横着的杆子上爬行,其速度均为1cm/s,蚂蚁到了杆子两头时就会掉下去。蚂蚁如果在爬行途中相遇,则两只蚂蚁都会逆转方向。已知蚂蚁在棒子的最初位置坐标,但是我们不知道他们会往哪一个方向爬。请求出所有蚂蚁掉下去的最短时间和最长时间。

问题分析

  对一个一个蚂蚁进行穷举搜索显然不是一个好主意。

  蚂蚁在位置p时,若往左其距离为p,若往右则其距离为l-p。

  蚂蚁碰头后,仍然是一只往左另一只往右,所以可以看作是各自继续行走。

  考虑最短时间,则对所有蚂蚁而言,每只蚂蚁取往左或往右取最短时间,然后从中找出最长时间。

  考虑最长时间,则对所有蚂蚁而言,每只蚂蚁取往左或往右取最长时间,然后从中找出最长时间。

程序说明

  全部使用C++的输入输出,有可能LTE,需要注意。

  程序逻辑尽可能简洁,使用数组存储数据也是没有必要的。

  程序中,使用了库函数max()和min(),使得程序代码十分简洁。

参考链接:(略)

题记:程序要简洁简洁再简洁。


AC的C++程序如下:

/* POJ1852 UVa10714 ZOJ2376 Ants */

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int t, l, n, pos, minans, maxans;

    scanf("%d", &t);;
    while(t--) {
        minans = 0;
        maxans = 0;

        scanf("%d%d", &l, &n);
        for(int i=1; i<=n; i++) {
            scanf("%d", &pos);

            minans = max(minans, min(pos, l - pos));

            maxans = max(maxans, max(pos, l - pos));
        }

        printf("%d %d\n", minans, maxans);
    }

    return 0;
}



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值