#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;
class Point {
public:
double x, y;
Point(double x1 = 0, double y1 = 0) : x(x1), y(y1){
}
};
class Node {
public:
double v;
int idx;
Node(double v1 = 0, int i1 = 0):v(v1), idx(i1){
}
};
class cmp{
public:
bool operator()(const Node& a, const Node& b) {
return a.v < b.v;
}
};
vector<double> cal(vector<Node>& p, vector<Node>& r) {
int ps = p.size(), rs = r.size(), i, j;
vector<double> res(rs, 0);
sort(p.begin(), p.end(), cmp());
sort(r.begin(), r.end(), cmp());
double d = 0;
for (i = 0; i < ps; ++i)
d += abs(p[i].v - r[0].v);
res[r[0].idx] = d;
for (i = 1; i < rs; ++i) {
double p1 = 0, p2 = 0, p3 = 0,
int i1 = upper_bound(p.begin(), p.end(), r[i-1].v, cmp()) - p.begin();
int i2 = upper_bound(p.begin(), p.end(), r[i].v, cmp()) - p.begin();
if (i1 > 0) {
p1 = i1 * (r[i].v-r[i-1].v);
}
if (i2 > i1) {
double d1 = abs(r[i-1].v - p[i1].v);
double d2 = abs(r[i].v - p[i2-1].v);
p2 = (i2 - i1) * (d2 - d1);
}
if (i2 < ps) {
p3 = (ps - i2) * (r[i-1].v-r[i].v);
}
res[r[i].idx] = p1 + p2 + p3;
}
return res;
}
Point getPoint(vector<Point>& p, vector<Point>& r) {
vector<Node> px, py, rx, ry;
int ps = p.size(), rs = r.size(), i,j;
assert(ps != 0 && rs != 0);
for (i = 0; i < ps; ++i) {
px.push_back(Node(p[i].x, i));
py.push_back(Node(p[i].y, i));
}
for (i = 0; i < rs; ++i) {
rx.push_back(Node(r[i].x, i));
ry.push_back(Node(r[i].y, i));
}
vector<double> resx = cal(px, rx);
vector<double> resy = cal(px, rx);
double maxv = resx[0] + resy[0], maxi = 0;
for (i = 1; i < rs; ++i) {
maxv = max(maxv, resx[i] + resy[i]);
}
return maxv;
}
int main() {
Point persons[] = {Point(-1,-1), Point(1,1), Point(3,3), Point(5,5)};
Point rests[] = {Point(2,2), Point(-3,3), Point(6,6), Point(4,6)};
vector<Point> ps(persons, persons+sizeof(persons)/ sizeof(persons[0]));
vector<Point> rs(rests, rests+sizeof(rests)/ sizeof(rests[0]));
return 0;
}