为了以后搜索方便,注:这道题是凸包问题。用的是convex hull的扫描算法。
这道题是2D的convex hull问题,有很成熟的Monotone chain的解法,时间复杂度为O(nlogn)。主要是如何简洁正确地实现。USACO的伪代码和标准答案都非常繁琐。我在Quora(http://www.quora.com/TopCoder/What-is-the-most-beautiful-implementation-of-Convex-Hull-you-have-ever-seen)上面了这里的链接,非常简洁:http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain。有各种语言的实现,实在是无法写得更好了。这里也是参考了wikibooks上面的链接,希望以后有人能够更新到wikipedia上面。
/*
ID: thestor1
LANG: C++
TASK: fc
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <limits>
#include <iomanip>
using namespace std;
class Point{
public:
double x, y;
Point()
{
Point(0, 0);
}
Point (double x, double y)
{
this->x = x;
this->y = y;
}
bool operator <(const Point &p) const {
return x < p.x || (x == p.x && y < p.y);
}
friend ostream& operator<<(ostream& os, const Point& p);
};
ostream& operator<<(ostream& os, const Point& p)
{
os << p.x << "\t" << p.y;
return os;
}
double cross(const Point &left, Point &middle, Point &right)
{
return (left.x - middle.x) * (right.y - middle.y) - (right.x - middle.x) * (left.y - middle.y);
}
double crossProduct(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
double dis(Point p1, Point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
int main()
{
ifstream fin("fc.in");
ofstream fout("fc.out");
int N;
fin>>N;
std::vector<Point> points;
for (int i = 0; i < N; ++i)
{
double x, y;
fin>>x>>y;
points.push_back(Point(x, y));
}
if (N <= 1)
{
fout<<"0.00"<<endl;
}
else
{
sort(points.begin(), points.end());
// for (int i = 0; i < points.size(); ++i)
// {
// cout<<points[i]<<endl;
// }
std::vector<int> convexhull(2 * points.size());
int k = 0;
for (int i = 0; i < points.size(); ++i)
{
while (k >= 2 && cross(points[convexhull[k - 2]], points[convexhull[k - 1]], points[i]) <= 0)
{
k--;
}
convexhull[k] = i;
k++;
}
for (int i = points.size() - 2, t = k + 1; i >= 0; --i)
{
while (k >= t && cross(points[convexhull[k - 2]], points[convexhull[k - 1]], points[i]) <= 0)
{
k--;
}
convexhull[k] = i;
k++;
}
convexhull.resize(k);
cout<<"convexhull:"<<endl;
for (int i = 0; i < convexhull.size(); ++i)
{
cout<<points[convexhull[i]]<<endl;
}
double perimeter = 0.00;
for (int i = 0; i < convexhull.size() - 1; ++i)
{
perimeter += dis(points[convexhull[i]], points[convexhull[i + 1]]);
}
fout<<std::setprecision(2)<<std::fixed<<perimeter<<endl;
}
fin.close();
fout.close();
return 0;
}
python画图,做调试用,其中fc.in是原程序输入,“convexhull.in”是生成出来的convex hull,格式为x1 y1\nx2 y2\n...
import matplotlib.pyplot as plt
with open("fc.in") as f:
n = int(f.readline())
for i in range(n):
x, y = [float(x) for x in f.readline().split()]
plt.plot(x, y, 'ro')
# plt.text(x, y, str((x, y)))
with open("convexhull.in") as f:
xs, ys = [], []
for line in f:
x, y = [float(x) for x in line.split()]
plt.text(x, y, str((x, y)))
xs.append(x)
ys.append(y)
plt.plot(xs, ys, '-')
plt.show()