UVaOJ 152 - Tree's a Crowd
来源:互联网 发布:车铣复合手工编程教程 编辑:程序博客网 时间:2024/06/04 19:50
AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving ::Sorting/Searching
Description
在一个三维直角坐标系中, 存在 n 个点。
对于某个点, 设离它最近的点的距离为 dis 。
给出所有点的坐标, 要求求出 dis 在1~10以内的点的数量。
Type
Sorting/Searching
Analysis
n <= 5000, 时限 3s。
直接用 O(n^2) 的暴力,居然过了。
Solution
// UVaOJ 152// Tree's a Crowd// by A Code Rabbit#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 5002;const int MAXR = 257;struct Point { int x, y, z;};Point pt[MAXN];bool bo[MAXR][MAXR][MAXR];int ans[12];int GetSqrDis(Point a, Point b);void PushAns(int dis, int idx);int main() { // Input. memset(bo, false, sizeof(bo)); int top = 0; while (scanf("%d%d%d", &pt[top].x, &pt[top].y, &pt[top].z)) { if (pt[top].x || pt[top].y || pt[top].z) { bo[pt[top].x][pt[top].y][pt[top].z] = true; top++; } else { break; } } // Solve. memset(ans, 0, sizeof(ans)); for (int i = 0; i < top; i++) { int min_sqr_dis = 100; for (int j = 0; j < top; j++) if (i != j) min_sqr_dis = min(GetSqrDis(pt[i], pt[j]), min_sqr_dis); PushAns(min_sqr_dis, 10); } // Output. for (int i = 0; i < 10; i++) printf("%4d", ans[i]); puts(""); return 0;}int GetSqrDis(Point a, Point b) { return pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2);}void PushAns(int dis, int idx) { if (dis >= idx * idx) ans[idx]++; else PushAns(dis, idx - 1);}
- UVaOJ 152 - Tree's a Crowd
- 152 - Tree's a Crowd
- 152 - Tree's a Crowd
- 152-Tree's a Crowd
- 152 - Tree's a Crowd
- 152 - Tree's a Crowd
- Tree's a Crowd
- Tree's a Crowd
- Tree's a Crowd
- UVa OJ 152-Tree's a Crowd
- uva 152 Tree's a Crowd
- UVA 152 - Tree's a Crowd
- UVa 152 - Tree's a Crowd
- uva 152 - Tree's a Crowd
- UVA-152- Tree's a Crowd
- UVA 152 - Tree's a Crowd
- UVa 152 - Tree's a Crowd
- uva 152 Tree's a Crowd
- 论述如何用UML进行系统分析与设计建模
- wctomb宽字符到多字节字符节的转换
- Jpa: Spring-based differ Container-based
- js中的关键字总结呢
- 设计模式-Prototype模式
- UVaOJ 152 - Tree's a Crowd
- Simple Addition Expression(排列组合)
- Linux VPS/服务器上用Crontab来实现VPS自动化
- abstract class和interface有什么区别?
- 一个可以放入STL容器的简洁、高效、非计数的智能指针实现
- String 和StringBuffer的区别
- 导入android系统隐藏包
- Linux下的hosts文件和network文件区别
- 四个数的最大公约数