山東省第1回ACM大学生プログラム設計コンテストShopping水....

2015 ワード

Shopping
Time Limit:1000 ms Memory limit:65536 K質問は?ここをクリック^^;
タイトルの説明
Saya and Kudo go shopping together. You can assume the street as a straight line, while the shops are some points on the line. They park their car at the leftmost shop, visit all the shops from left to right, and go back to their car. Your task is to calculate the length of their route.
入力
The input consists of several test cases. The first line of input in each test case contains one integer N (0しゅつりょく
 For each test case, print the length of their shopping route.
サンプル入力
4
24 13 89 37
6
7 30 41 14 39 42
0

サンプル出力
152
70

ヒント
Explanation for the first sample: They park their car at shop 13; go to shop 24, 37 and 89 and finally return to shop 13. The total length is (24-13) + (37-24) + (89-37) + (89-13) = 152
ソース
2010年山東省第1回ACM大学生プログラム設計コンテスト
ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int main(){
    int maxx,minn,n,a;
    while(~scanf("%d",&n)&&n){
        maxx=-inf;
        minn=inf;
        while(n--){
            scanf("%d",&a);
            maxx=maxx>a?maxx:a;
            minn=minn<a?minn:a;
        }
        printf("%d
",(maxx-minn)<<1); } return 0; } /* 4 24 13 89 37 6 7 30 41 14 39 42 0 */