-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathManhattan 2D farthest points.cpp
More file actions
71 lines (62 loc) · 1.54 KB
/
Manhattan 2D farthest points.cpp
File metadata and controls
71 lines (62 loc) · 1.54 KB
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
66
67
68
69
70
71
#include <bits/stdc++.h>
// Surely, the solution can be adapted to larger dimensions.
using namespace std;
const int me = 100025;
struct point{
vector<int> x;
point() {}
point(vector<int> x) : x(x) {}
bool operator ==(const point other) const{
return x == other.x;
}
bool operator <(const point other) const{
return x < other.x;
}
int dis(const point other){
int d = 0;
for(int i = 0; i < x.size(); i ++)
d += abs(x[i] - other.x[i]);
return d;
}
};
int n, m, ans;
point a[me];
set<point> best;
int main()
{
//ios_base::sync_with_stdio(0);
//cin.tie(0);
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i ++){
a[i].x.resize(m);
for(int j = 0; j < m; j ++)
scanf("%d", &a[i].x[j]);
}
for(int i = 0; i < (1 << m); i ++){
int far_d = 0, near_d = 0;
int far_i = 0, near_i = 0;
for(int j = 0; j < n; j ++){
int s = 0;
for(int k = 0; k < m; k ++){
if(i & (1 << k))
s += a[j].x[k];
else s -= a[j].x[k];
}
if(s > far_d){
far_d = s;
far_i = j;
}
if(s < near_d){
near_d = s;
near_i = j;
}
}
best.insert(a[far_i]);
best.insert(a[near_i]);
}
for(int i = 0; i < n; i ++)
for(auto j : best)
ans = max(ans, a[i].dis(j));
cout << ans << endl;
return 0;
}