您的位置:首页 > 其它

poj_3632 摸爬滚打在水题中

2011-05-06 01:55 465 查看
Optimal Parking

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6073Accepted: 3383
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

输入的n的数是随机的,只要将他们排序后,在数据段之间任意位置停车所走的路程都相等,即为所有相邻两点的差的总和,因为要有来回,最后X2#include<iostream>using namespace std;int main(){         int i,j,sum,t,*a,n;         cin>>t;         while(t--)         {                   cin>>n;                   a=new int
;                   for(i=0;i<n;i++)                            cin>>a[i];                   for(i=1;i<n;i++)                            for(j=0;j<n-i;j++)                            {                                     if(a[j]>a[j+1])                                     {                                               sum=a[j];                                               a[j]=a[j+1];                                               a[j+1]=sum;                                     }                            }                   sum=0;                   for(i=1;i<n;i++)                            sum+=a[i]-a[i-1];                   delete []a;                   cout<<2*sum<<endl;         }         return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: