-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10041.cpp
executable file
·65 lines (55 loc) · 1.39 KB
/
10041.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Problem: Vito's family UVa 10041
Programmer: Md. Mahmud Ahsan
Compiled: Visual C++ 6.0
Date: 07-10-04
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
//typedef type
typedef long int lint;
void insertion(lint array[], lint size);
//================================================================================
int main(){
lint test;
lint relative;
lint dataSet[30005];
/* minSum = minimal sum of distances
optimal = optimal here means middle of the street
*/
lint optimal, minSum, temp;
int i, j;
cin >> test;
for (i = 0; i < test; i++){
minSum = 0;
for (j = 0; j < 30005; j++)
dataSet[j] = 0;
cin >> relative;
for (j = 0; j < relative; j++)
cin >> dataSet[j];
// street numbers must be sorted
insertion(dataSet, relative);
optimal = relative / 2;
for (j = 0; j < relative; j++){
// distance between two street number d[i][j] = abs(s[i] - s[j])
temp = abs (dataSet[optimal] - dataSet[j]);
minSum += temp;
}
cout << minSum << endl;
}
return 0;
}
//===============================================================================
void insertion(lint array[], lint size){
lint i, j, key;
for (j = 1; j < size; j++){
key = array[j];
i = j - 1;
while (i >= 0 && array[i] > key){
array[i+1] = array[i];
i = i - 1;
}
array[i+1] = key;
}
}