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;
}
}
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;
}
}
相关文章推荐
- 微软Word 2007数学插件 Microsoft Math 提供下载
- RM格式压缩电影软件 Easy RealMedia Producer Full V1.94 下载
- 关于C# Math 处理奇进偶不进的实现代码
- JavaScript中的Math.SQRT1_2属性使用简介
- JavaScript Math.ceil() 函数使用介绍
- 与Math.pow 相反的函数使用介绍
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- JavaScript中使用指数方法Math.exp()的简介
- 比较简单的jquery教程 Easy Ajax with jQuery 中文版全集第1/3页
- Easy RM RMVB to DVD Burner v1.3.8 汉化版 下载 附注册码
- JavaScript使用Math.Min返回两个数中较小数的方法
- Ajax获取页面被缓存的解决方法
- JavaScript高级程序设计 阅读笔记(十二) js内置对象Math
- JavaScript中Math对象使用说明
- javascript对象之内置对象Math使用方法
- 基于java math API 的详细解释说明
- 简介JavaScript中用于处理正切的Math.tan()方法
- 浅谈JavaScript中的Math.atan()方法的使用
- PHP内置的Math函数效率测试
- 在JavaScript中使用对数Math.log()方法的教程