您的位置:首页 > 其它

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

2016-02-14 00:06 302 查看
&
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BestCoder hdoj math easy