-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0104.h
More file actions
42 lines (36 loc) · 908 Bytes
/
Problem0104.h
File metadata and controls
42 lines (36 loc) · 908 Bytes
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
//
// Created by Fengwei Zhang on 2021/6/26.
//
#ifndef ACWINGSOLUTION_PROBLEM0104_H
#define ACWINGSOLUTION_PROBLEM0104_H
#include <iostream>
#include <algorithm>
using namespace std;
class Problem0104
{
private:
long min_distance(int *coordinates, const int &n)
{
sort(coordinates, coordinates + n);
const int mid = n >> 1; // 无论mid偏左或偏右(长度为偶数),结果都是正确的
long result = 0;
for (int i = 0; i < n; ++i)
{
result += abs(coordinates[i] - coordinates[mid]);
}
return result;
}
int main()
{
int n;
scanf("%d", &n);
int coordinates[n];
for (int i = 0; i < n; ++i)
{
scanf("%d", &coordinates[i]);
}
printf("%ld\n", min_distance(coordinates, n));
return 0;
}
};
#endif // ACWINGSOLUTION_PROBLEM0104_H