poj_3632 摸爬滚打在水题中
2011-05-06 01:55
465 查看
Optimal Parking
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6073 | Accepted: 3383 |
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;}
相关文章推荐
- POJ 3628 Bookshelf 2 0-1背包 or DFS 水题
- poj2014-模拟----水题1A
- poj 2262 Goldbach's Conjecture 素数 水题
- POJ 1005 I Think I Need a Houseboat (水题)
- poj_2013 水题
- Poj 1094 拓扑排序 水题
- POJ 3624 _01 背包水题!
- poj 3750 水题啊。。。。。。
- POJ 3312(水题)
- POJ 2291水题
- POJ 3481 Double Queue (Splay || 水题)
- poj 2499 Binary Tree(水题)
- HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算+水题】
- poj2407——水题
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- POJ 1504 (一个水题引发的血案)
- poj1068 Parencodings 模拟水题
- POJ 1493 Machined Surfaces(我的水题之路——移动后的空格)
- POJ 2000 Glod Conins(我的水题之路——之后的N天N个金币,打表)
- POJ 1067(广搜水题)