/home/toolbox/public_html/solutions/100/10041/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 #define MAXPOINTS 501
16
17
18 int cnt;
19 int numStreets;
20 int streets[MAXPOINTS];
21
22 /*
23 * Author: Isaac Traxler
24 * Date: 2015 02 10
25 * Purpose:
26 * Problem: 10041
27 */
28
29 void init()
30 {
31 /* FUNCTION init */
32 scanf(" %d ", &cnt);
33 } /* FUNCTION init */
34
35 void dump()
36 {
37 /* FUNCTION dump */
38 } /* FUNCTION dump */
39
40 int compare(const void *a, const void *b)
41 {
42 /* FUNCTION compare */
43 return ( *(int*)a - *(int*)b );
44 } /* FUNCTION compare */
45
46 void getInput()
47 {
48 /* FUNCTION getInput */
49 int i;
50
51 scanf(" %d ", &numStreets);
52 for (i=0; i<numStreets; i++)
53 {
54 /* for */
55 scanf(" %d ", &streets[i]);
56 } /* for */
57 qsort(streets, numStreets, sizeof(int), compare);
58 } /* FUNCTION getInput */
59
60 int sumEm(int x)
61 {
62 /* FUNCTION sumEm */
63 int i;
64 int tot = 0;
65
66 for (i=0; i<numStreets; i++)
67 {
68 /* for */
69 DEBUG printf("tot = %d streets[%d] = %d streets[%d] = %d\n", tot, i, streets[i], x, streets[x]);
70 tot = tot + abs(streets[i] - streets[x]);
71 } /* for */
72
73 return tot;
74 } /* FUNCTION sumEm */
75
76 void process()
77 {
78 /* FUNCTION process */
79 int i;
80 int tot;
81
82 i = (numStreets -1) / 2;
83 DEBUG printf("numStreets =%d streets[%d] = %d streets[%d] = %d\n", numStreets, i, streets[i], i+1, streets[i+1]);
84 tot = sumEm(i);
85 printf("%d\n", tot);
86 } /* FUNCTION process */
87
88 int main ()
89 {
90 /* main */
91 int i;
92
93 init();
94 for (i=0; i<cnt; i++)
95 {
96 /* for */
97 getInput();
98 process();
99 } /* for */
100
101 return EXIT_SUCCESS;
102 } /* main */
103
104
105