#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <limits>
#include <algorithm>
using namespace std;
struct Point
{
double x;
double y;
};
vector<Point> point;
bool compareX(const Point& x, const Point& y)
{
return (x.x < y.x || (x.x == y.x && x.y < y.y));
}
bool compareY(const int& x, const int& y)
{
return point[x].y < point[y].y;
}
class QuoitDesign
{
public:
void initialize();
void readCase(int& n);
void compute();
void output();
private:
double distance(const int& x, const int& y)
{
return sqrt((point[x].x - point[y].x) * (point[x].x - point[y].x) + (point[x].y - point[y].y) * (point[x].y - point[y].y));
}
double findShort(const int& left, const int& right);
private:
double radius;
};
void QuoitDesign::initialize()
{
point.clear();
}
void QuoitDesign::readCase(int& n)
{
while(n--)
{
Point p;
cin >> p.x >> p.y;
point.push_back(p);
}
}
void QuoitDesign::compute()
{
sort(point.begin(), point.end(), compareX);// 根据点的y值和x值排序
radius = this->findShort(0, point.size()) / 2;
}
double QuoitDesign::findShort(const int& left, const int& right)
{
double minLine = numeric_limits<double>::max();
if(left == right)
{
return minLine;
}
if(left + 1 == right)
{
return this->distance(left, right);
}
int middle = (left + right) >> 1;// 找出中线
double leftLine = this->findShort(left, middle);
double rightLine = this->findShort(middle + 1, right);
minLine = min(leftLine, rightLine);
vector<int> temp;
temp.clear();
for(int i = left; i < right; ++i)
{
if((point[middle].x - point[i].x <= minLine && point[middle].x - point[i].x >= -minLine))// 分离范围不然会超时
{
temp.push_back(i);
}
}
sort(temp.begin(), temp.end(), compareY);
for(int i = 0; i < temp.size(); ++i)
{
for(int j = i + 1; j < temp.size() && point[temp[j]].y - point[temp[i]].y <= minLine; ++j)
{
double r = this->distance(temp[i], temp[j]);
if(minLine > r)
{
minLine = r;
}
}
}
return minLine;
}
void QuoitDesign::output()
{
cout << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << radius << endl;
}
int main()
{
QuoitDesign quoitDesign;
int n;
while(cin >> n && n != 0)
{
quoitDesign.initialize();
quoitDesign.readCase(n);
quoitDesign.compute();
quoitDesign.output();
}
return 0;
}
hdu 1007 Quoit Design
最新推荐文章于 2021-03-22 21:08:16 发布