BestCoder Round #72 (div.2) B.Clarke and points
来源:互联网 发布:笑傲江湖 朝代 知乎 编辑:程序博客网 时间:2024/05/22 08:10
& 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
- BestCoder Round #72 (div.2) B.Clarke and points
- BestCoder Round #72 (div.2) A.Clarke and chemistry
- BestCoder Round #72 (div.2) Clarke and math
- BestCoder Round #72 (div.2)-Clarke and chemistry(暴力枚举)
- bestcoder #72Clarke and points
- BestCoder Round #72 (div.1) Clarke and MST
- hdu5463 Clarke and minecraft(BestCoder Round #56 (div.2) )
- BestCoder Round #56 (div.2)(Clarke and minecraft)
- BestCoder Round #56 (div.2)(Clarke and problem)
- BestCoder Round #56 (div.2) HDU5463 Clarke and minecraft
- hdu 5464 Clarke and problem (BestCoder Round #56 (div.2))
- BestCoder Round #56 (div.2) HDU5463 Clarke and minecraft
- BestCoder Round #62 (div.2)-Clarke and food(模拟)
- BestCoder Round #62 (div.2) 1001 Clarke and food
- BestCoder Round #62 (div.2) HDU 5562 Clarke and food
- BestCoder Round #62 (div.2) Clarke and food
- BestCoder Round #62 (div.2) Clarke and five-pointed star
- BestCoder Round #62 (div.2) D.Clarke and baton
- windows系统 配置apache2.2+php5.2+mysql5.5
- JS中的事件捕获和事件冒泡
- PAT_乙级1009
- [BZOJ1014] [JSOI2008]火星人prefix
- JSP生成验证码
- BestCoder Round #72 (div.2) B.Clarke and points
- POJ 2182 Lost Cows
- 多态思想并举例
- socket编程 -- 大端小端区别及转换
- HDU 5625 Clarke and chemistry(模拟)
- UIScrollView
- TP-LINK 桥接
- Bellman-Ford Algorithm and Dijkstra Algorithm
- Ubuntu 下安装 Nginx ( Nginx + Php5-fpm )