-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOil.c
46 lines (43 loc) · 881 Bytes
/
Oil.c
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
#include<stdio.h>
void swap(int A[], int i, int j);
void quicksort(int A[], int i, int j);
int main(void){
int n,x,y;
int sum = 0;
scanf("%d", &n);
int Location[10];
for (int i = 0; i < n; i++){
scanf("%d%d", &x, &y);
Location[i] = y;
}
quicksort(Location, 0, n - 1);
for (int i = 0; i < n / 2; i++){
sum += Location[n / 2] - Location[i];
}
for (int i = n / 2; i < n; i++){
sum += Location[i] - Location[n /2];
}
printf("%d",sum);
return 0;
}
void quicksort(int A[], int i, int j){
if(j <= i) return;
int pivotindex = (j + i) / 2;
swap(A, pivotindex, j);
int l = i - 1;
int r = j;
int pivot = A[j];
do{
while (A[++l] < pivot);
while ((l < r) && (pivot < A[--r]));
swap(A, l, r);
}while (l < r);
swap(A,l,j);
quicksort(A, i, l - 1);
quicksort(A, l + 1, j);
}
void swap(int A[], int i ,int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}