HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2 曼哈顿距离 与 切比雪夫距离

来源:互联网 发布:php 手动gc 编辑:程序博客网 时间:2024/04/26 06:40

题目大意:

给出平面上N个整点的坐标, N <= 100000, 横纵坐标的绝对值都不超过1e9

求以那个点为中心使得其他点到这个点的距离总和最小

HDU 4311要求曼哈顿距离最小 两个点(x1, y1), (x2, y2)的曼哈顿距离为 |x1 - x2| + |y1 - y2|

HDU 4312要求切比雪夫距离最小, 两个点(x1, y1), (x2, y2)的切比雪夫距离为 max(|x1 - x2|, |y1 - y2|)


大致思路:

手续爱你对于HDU 4311要求曼哈顿距离最小

注意到对于曼哈顿距离实际上横坐标造成的和纵坐标造成的和是可以分开算的

于是首先对横坐标排序求出前缀和, 对纵坐标排序也求出前缀和, 但是同时记录纵坐标排序是第几位

那么按横坐标排序后第p个点的贡献为 (p - 1)*xp -(x1 + x2 +...+x_(p - 1)) - (n - p)*xp + (x_(p + 1)....xn)

对于纵坐标表达式也类似, 于是就很容易写了, 具体细节看代码吧, 并不难


对于HDU 4312要求切比雪夫距离

注意到max(|x1 - x2|, |y1 - y2|) = (|x1 - x2 + (y1 - y2)| + |x1 - x2 - (y1 - y2)|) / 2 = |(x1 + y1)/2 - (x2 + y2)/2| + |(x1 - y1)/2 - (x2 -- y2)/2|

于是点(x1, y1)和点(x2, y2)的距离就是点((x1 + y1)/2, (x1 - y1)/2) 和点((x2 + y2) / 2, (x2 - y2) / 2)之间的曼哈顿距离

所以对于所有的点的坐标映射一下就好了, 就和HDU 4311一样了

为了避免浮点数没有除以2, 在最后结果上才除以了2


代码如下:

HDU 4311:

Result  :  Accepted     Memory  :  5544 KB     Time  :  452 ms

/* * Author: Gatevin * Created Time:  2015/7/31 23:18:02 * File Name: Sakura_Chiyo.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010struct point{    lint x, y;    int ranky;    point(){}    point(lint _x, lint _y, int _ranky) : x(_x), y(_y), ranky(_ranky){}};int n;point p[maxn];bool cmpx(point p1, point p2){    if(p1.x == p2.x) return p1.y < p2.y;    else return p1.x < p2.x;}bool cmpy(point p1, point p2){    if(p1.y == p2.y) return p1.x < p2.x;    else return p1.y < p2.y;}lint sumx[maxn], sumy[maxn];void change(lint &x, lint &y){    lint tx = (x + y);    lint ty = (x - y);    x = tx, y = ty;    return;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        for(int i = 0; i < n; i++)        {            scanf("%I64d %I64d", &p[i].x, &p[i].y);            change(p[i].x, p[i].y);        }        sort(p, p + n, cmpy);        sumy[0] = p[0].y; p[0].ranky = 0;        for(int i = 1; i < n; i++) sumy[i] = sumy[i - 1] + p[i].y, p[i].ranky = i;        sort(p, p + n, cmpx);        sumx[0] = p[0].x;        for(int i = 1; i < n; i++) sumx[i] = sumx[i - 1] + p[i].x;        lint ans = 1e18;        for(int i = 0; i < n; i++)//以第i个点作为答案        {            lint tmp = (2*i - n + 1)*p[i].x;            if(i > 0) tmp -= sumx[i - 1];            tmp += sumx[n - 1] - sumx[i];            int ny = p[i].ranky;            tmp += (2*ny - n + 1)*p[i].y;            if(ny > 0) tmp -= sumy[ny - 1];            tmp += sumy[n - 1] - sumy[ny];            ans = min(ans, tmp);        }        printf("%I64d\n", ans/2);    }    return 0;}



HDU 4312:

Result  :  Accepted     Memory  :  5544 KB     Time  :  436 ms

/* * Author: Gatevin * Created Time:  2015/7/31 23:18:02 * File Name: Sakura_Chiyo.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010struct point{    lint x, y;    int ranky;    point(){}    point(lint _x, lint _y, int _ranky) : x(_x), y(_y), ranky(_ranky){}};int n;point p[maxn];bool cmpx(point p1, point p2){    if(p1.x == p2.x) return p1.y < p2.y;    else return p1.x < p2.x;}bool cmpy(point p1, point p2){    if(p1.y == p2.y) return p1.x < p2.x;    else return p1.y < p2.y;}lint sumx[maxn], sumy[maxn];void change(lint &x, lint &y){    lint tx = (x + y);    lint ty = (x - y);    x = tx, y = ty;    return;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        for(int i = 0; i < n; i++)        {            scanf("%I64d %I64d", &p[i].x, &p[i].y);            change(p[i].x, p[i].y);        }        sort(p, p + n, cmpy);        sumy[0] = p[0].y; p[0].ranky = 0;        for(int i = 1; i < n; i++) sumy[i] = sumy[i - 1] + p[i].y, p[i].ranky = i;        sort(p, p + n, cmpx);        sumx[0] = p[0].x;        for(int i = 1; i < n; i++) sumx[i] = sumx[i - 1] + p[i].x;        lint ans = 1e18;        for(int i = 0; i < n; i++)//以第i个点作为答案        {            lint tmp = (2*i - n + 1)*p[i].x;            if(i > 0) tmp -= sumx[i - 1];            tmp += sumx[n - 1] - sumx[i];            int ny = p[i].ranky;            tmp += (2*ny - n + 1)*p[i].y;            if(ny > 0) tmp -= sumy[ny - 1];            tmp += sumy[n - 1] - sumy[ny];            ans = min(ans, tmp);        }        printf("%I64d\n", ans/2);    }    return 0;}


0 0
原创粉丝点击