代码编织梦想

题目描述

When shopping on Long Street, Michael usually parks his car at some random location, and then walks to the stores he needs. Can you help Michael choose a place to park which minimises the distance he needs to walk on his shopping round? Long Street is a straight line, where all positions are integer. You pay for parking in a specific slot, which is an integer position on Long Street. Michael does not want to pay for more than one parking though. He is very strong, and does not mind carrying all the bags around.

输入要求

The first line of input gives the number of test cases, 1 ≤ t ≤ 100. There are two lines for each test case. The first gives the number of stores Michael wants to visit, 1 ≤ n ≤ 20, and the second gives their n integer positions on Long Street, 0 ≤ xi ≤ 99.

输出要求

Output for each test case a line with the minimal distance Michael must walk given optimal parking.

输入样例

2

4

24 13 89 37

6

7 30 41 14 39 42

输出样例

152

70

#include <stdio.h>

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        int min = 100;
        int max = 0;
        for (int i = 0; i < n; i++) {
            int x;
            scanf("%d", &x);
            if (x < min) min = x;
            if (x > max) max = x;
        }
        printf("%d\n", (max - min) * 2);
    }
    return 0;
}

总结:

这段代码中,我们首先输入测试用例数t,然后对每个测试用例进行处理。

对于每个测试用例,我们首先输入商店数量n,然后输入每个商店的位置。在输入过程中,我们记录所有商店位置的最小值和最大值。

根据上面的分析,Michael应该将车停在所有商店位置的中间。因此,他需要走到最左边或最右边的商店(距离为max - min),然后依次访问剩下的商店(距离为max - min),最后回到停车位置(距离为max - min)。所以总共需要走的距离为(max - min) * 2

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34106320/article/details/129740758

杭电 hdu acm 1673 optimal parking-爱代码爱编程

Optimal Parking Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1942    Accepted Submission(s): 1624 Problem Descr

南邮 oj 1524 optimal parking-爱代码爱编程

Optimal Parking 时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte 总提交 : 23            测试通过 : 21  比赛描述 When shopping on Long Street,

[sicily]1433. optimal parking-爱代码爱编程

1433. Optimal Parking Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description When shopping on Long Street, Michael usually parks his car at

hdu 1673 optimal parking(看懂就是水题)-爱代码爱编程

Optimal Parking Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2259    Accepted Submission(s): 1884 Problem Desc

hdoj1673 optimal parking_逸川先生的博客-爱代码爱编程

Optimal Parking Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2862    Accepted Submission(s): 2376 Problem Descr

Optimal Parking-爱代码爱编程

Optimal Parking | JXNUOJ  翻译: 描述: 在长街购物时,迈克尔通常将车停在某个随机位置,然后步行到他需要的商店。 你能帮助迈克尔选择一个可以最大限度地减少他在购物时步行距离的停车地点吗? 长街是一条直线,所有位置都是整数。 您支付特定位置的停车费,该位置是长街上的整数位置。 不过,迈克尔不想为多个停车位付费。 他很

论文笔记:improved hierarchical a-star algorithm for optimal parking path planning of the large parking_naglio_的博客-爱代码爱编程

  参数解释: P:停车位(节点) C:路口或交差节点   (节点) Dk(i, j):第k条路段从第i个节点到第j个节点的线性距离 Vk(i, j):第k条路段车辆从第i个节点到第j个节点的平均速度 问题描述: 在该网络图中(R(P,C,D,V))找到入口(S或e)到停车位(P)的路径(停车引导系统 最优路径规划) 算法:

【1630. 等差子数组】-爱代码爱编程

来源:力扣(LeetCode) 描述: 如果一个数列由至少两个元素组成,且每两个连续元素之间的差值都相同,那么这个序列就是 等差数列 。更正式地,数列 s 是等差数列,只需要满足:对于每个有效的 i , s[i+1] -

buu(前三页第三弹) rsa习题与相关知识总结_libnum.np-爱代码爱编程

文章目录 [BJDCTF2020]rsa_output 1:题目描述:题目分析:收获与体会: SameMod 1题目描述:题目分析:收获与体会: buu [BJDCTF2020]ea

代码随想录第二十七天(669、108、538、回溯算法介绍)-爱代码爱编程

669. 修剪二叉搜索树 不能简单地通过递归实现代码,比如: class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int hig