hdoj1673

原文:

Problem Description
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.
Input
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
Output for each test case a line with the minimal distance Michael must walk given optimal parking.
Sample Input
2
4
24 13 89 37
6
7 30 41 14 39 42
Sample Output
152
70

題意理解:

這道題原文有一點(diǎn)坑,開始時(shí)不一定能讀懂,但讀懂以后就會(huì)覺得特別簡(jiǎn)單。
題意大致如下:
有n個(gè)地點(diǎn),每個(gè)地點(diǎn)的位置都在同一條直道上,有個(gè)人把車停在某一個(gè)地點(diǎn),然后他去那條直道上所有的n個(gè)點(diǎn)購(gòu)物,最后回到停車的地點(diǎn)。問在最佳停車點(diǎn)的情況下那個(gè)人需要步行的最小距離。
事實(shí)上,如果你把線段圖畫出來(lái)的話,你就會(huì)發(fā)現(xiàn):不管怎樣,最小的距離就是最大距離點(diǎn)和最小距離點(diǎn)的值之差再乘以2.

參考代碼如下:

#include <iostream>
#include <string>
using namespace std;
int position[22];
int min_d(int *p,int n) {
    int max = *p,min = *p;
    int distance;
    int *pi;
    for (pi = p;pi < p+n;pi++) {
        if (*pi > max) {
            max = *pi;
        }
        else if (*pi < min) {
            min = *pi;
        }
    }
    distance = (max - min) * 2;
    return distance;
}
int main() {
    int t;
    int n;
    int distance;
    cin >> t;
    while (t--) {
        cin >> n;
        memset(position,0,sizeof(position));
        for (int i = 0;i < n;i++) {
            cin >> position[i];
        }
        distance = min_d(position,n);
        cout << distance << endl;
    }
    return 0;
}

如果你對(duì)我的博客有什么建議的,歡迎交流。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,118評(píng)論 0 23
  • 在某群看到一張幾個(gè)孩子站成一排的照片,沒做作業(yè),老師要求家長(zhǎng)各自認(rèn)領(lǐng),瞬間一股復(fù)雜的情緒冒出來(lái),各種悲傷、...
    是意空間閱讀 607評(píng)論 0 3
  • 長(zhǎng)大的牧童 拍著老黃牛 真辛苦你了
    長(zhǎng)馬閱讀 93評(píng)論 0 2
  • 不以物喜,不以已悲,不以情歡,不以離苦。值此人生之境,無(wú)為而無(wú)不為,無(wú)思而無(wú)不思,無(wú)欲而無(wú)不欲,無(wú)求而無(wú)不求。這時(shí)...
    花語(yǔ)_21b1閱讀 227評(píng)論 0 0
  • 又逢一歲秋光漾, 翠葉流金果錯(cuò)紛。 千架龍珠由客采, 淺紅深紫惜無(wú)君。
    A_master閱讀 167評(píng)論 0 3

友情鏈接更多精彩內(nèi)容