BestCoder Round #72 (div.2) B.Clarke and points

& hdoj 5626

题意:

平面上n个点,求两点间最大的曼哈顿距离。

题解:

假设A, B两点使得曼哈顿距离最大,去绝对值,可以化简如下:

则只要分别求出最大和最小的即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1000000 + 10;
struct point
{
    ll x, y;
}a[maxn];

ll seed;
inline long long Rand(long long l, long long r) {
	static long long mo=1e9+7, g=78125;
	return l+((seed*=g)%=mo)%(r-l+1);
}

int main()
{
    int t, n;

    cin >> t;
    while(t--)
    {
        cin >> n >> seed;
        for (int i = 0; i < n; i++){
            a[i].x = Rand(-1000000000, 1000000000),
            a[i].y = Rand(-1000000000, 1000000000);
        }

        ll xMax = a[0].x + a[0].y, xMin = a[0].x + a[0].y;
        ll yMax = a[0].x - a[0].x, yMin = a[0].x - a[0].y;
        for(int i = 1; i < n; ++i)
        {
            ll ax = a[i].x + a[i].y;
            ll in = a[i].x - a[i].y;
            xMax = max(xMax, ax);
            xMin = min(xMin, ax);
            yMax = max(yMax, in);
            yMin = min(yMin, in);
        }
        ll ans = max(abs(xMax - xMin), abs(yMax - yMin));
        printf("%I64d\n", ans);
    }

    return 0;
}


还有一种通过二进制运算来枚举这四种形式的,妙。

这种方法也很适合计算n维空间两点的曼哈顿距离。

参考点击打开链接

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1000000 + 10;
const ll inf = 1LL << 60;
const int dimension = 2;

long long seed;

inline long long Rand(long long l, long long r) {
    static long long mo=1e9+7, g=78125;
    return l+((seed*=g)%=mo)%(r-l+1);
}
struct point{
    ll x[dimension];
}p[maxn];

int main()
{
    int t, n;

    cin >> t;
    while(t--)
    {
        cin >> n >> seed;
        for (int i = 0; i < n; i++){
            p[i].x[0] = Rand(-1000000000, 1000000000),
            p[i].x[1] = Rand(-1000000000, 1000000000);
        }

        ll ans = 0;
        for(int s = 0; s < (1 << dimension); ++s)
        {
            ll ax = -inf, in = inf;
            for(int i = 0; i < n; ++i)
            {
                ll tmp = 0;
                for(int j = 0; j < dimension; ++j)
                {
                    if((1 << j) & s) tmp += p[i].x[j];
                    else tmp -= p[i].x[j];
                }
                ax = max(ax, tmp);
                in = min(in, tmp);
            }
            ans = max(ax - in, ans);
        }
        cout << ans << endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值