题意:顺时针方向给出N个点,求外围距离这些点L距离的点围成的图形的周长,结果四舍五入到整数(3 <= N <= 1000,1 <= L <= 1000,-10000 <= Xi, Yi <= 10000) 。
题目链接:http://poj.org/problem?id=1113
——>>先求凸包,然后求凸包的周长加上一个半径为L的圆的周长。
注意:用round()四舍五入后,若用%.0lf输出会WA,round()后强转为int后输出AC!
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int maxn = 1000 + 10; const double eps = 1e-10; const double pi = acos(-1); int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } struct Point{ double x; double y; Point(double x = 0, double y = 0):x(x), y(y){} bool operator < (const Point& e) const{ return x < e.x || (dcmp(x - e.x) == 0 && y < e.y); } }p[maxn], q[maxn]; typedef Point Vector; Vector operator + (Point A, Point B){ return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Point A, Point B){ return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Point A, double p){ return Vector(A.x * p, A.y * p); } Vector operator / (Point A, double p){ return Vector(A.x / p, A.y / p); } double Cross(Vector A, Vector B){ return A.x * B.y - B.x * A.y; } int ConvexHull(Point *p, int n, Point* ch){ //求凸包 sort(p, p + n); int m = 0; for(int i = 0; i < n; i++){ while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) < 0) m--; ch[m++] = p[i]; } int k = m; for(int i = n-2; i >= 0; i--){ while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) < 0) m--; ch[m++] = p[i]; } if(n > 1) m--; return m; } double Dis(Point A, Point B){ return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } int main() { int N, L; while(scanf("%d%d", &N, &L) == 2){ for(int i = 0; i < N; i++) scanf("%lf%lf", &p[i].x, &p[i].y); double ret = 0; int m = ConvexHull(p, N, q); for(int i = 1; i < m; i++) ret += Dis(q[i], q[i-1]); ret += Dis(q[0], q[m-1]) + 2 * pi * L; int ans = round(ret); printf("%d ", ans); } return 0; }