UVA_10245_ The Closest Pair Problem
2016-04-15 15:52
393 查看
#include<iostream> #include<sstream> #include<string> #include<vector> #include<list> #include<set> #include<map> #include<stack> #include<queue> #include<algorithm> #include<cmath> #pragma warning(disable:4996) using std::cin; using std::cout; using std::endl; using std::stringstream; using std::string; using std::vector; using std::list; using std::pair; using std::set; using std::multiset; using std::map; using std::multimap; using std::stack; using std::queue; double divide_conquer(vector<pair<double,double>>point) { if (point.size() == 1) { return 1000000000; } else if (point.size() == 2) { return sqrt(pow(point[0].first - point[1].first, 2) + pow(point[0].second - point[1].second, 2)); } vector<pair<double, double>> point1; point1.assign(point.begin(), point.begin()+point.size()/2); vector<pair<double, double>> point2; point2.assign(point.begin() + point.size() / 2, point.end()); auto min = std::min(divide_conquer(point1),divide_conquer(point2)); for (size_t i = 0; i < point.size() / 2; i++) { for (size_t j = point.size() / 2; j < point.size(); j++) { min = std::min(min, sqrt(pow(point[i].first-point[j].first,2)+ pow(point[i].second - point[j].second, 2))); } } return min; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; while (scanf("%d",&n)!=EOF&&n) { vector<pair<double, double>>point(n); for (int i = 0; i < n; i++) { scanf("%lf%lf", &point[i].first,&point[i].second); } /*std::sort(point.begin(), point.end()); auto result = divide_conquer(point);*/ double result = 10000.0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = std::min(result, sqrt(pow(point[i].first - point[j].first, 2) + pow(point[i].second - point[j].second, 2))); } } if (result >= 10000) { printf("INFINITY\n"); } else { printf("%.4lf\n", divide_conquer(point)); } } return 0; }
相关文章推荐
- 调用http://apistore.baidu.com网站的接口
- a、button、input点击出现蓝色边框,如何去掉
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
- 进程控制开发[fork() exec exit _exit wait waitpid 守护进程]
- 219. Contains Duplicate II
- 编译过程中,报failed to resolve:com.andriod.databinding.adapters:1.1
- Lesson 3: More About Jobs and Job Details
- textview 计算宽
- CDISC SDTM CM domain 学习笔记
- http://blog.csdn.net/zhou452840622/article/details/41820301
- URLDecoder: Incomplete trailing escape (%) pattern错误处理
- 查找字符串(containsString和rangeOfString的区别)
- CGRectContainsPoint 用法
- 国外braintree支付手段配置以及使用
- xcode7.3 iTunes Store operation failed问题
- arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)
- Pku oj 1218 THE DRUNK JAILER(开灯问题)
- “selection does not contain a main type”解决方法
- NLTEST to test the trust relationship between a workstation and domain
- WARNING: The first attempt to start Xvnc failed, possibly because the font