平面上有n个人,挑一家餐馆到这些人的曼哈顿距离最小

265 篇文章 1 订阅
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值