[codevs 1298] 凸包周长 [codevs 3201] 奶牛代理商 XI
生活随笔
收集整理的這篇文章主要介紹了
[codevs 1298] 凸包周长 [codevs 3201] 奶牛代理商 XI
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題解:
今天開始學習計算幾何。
這是一道計算幾何求凸包周長的模板題,采用Andrew算法。
第二道題改下輸出即可。
最后凸包周長的求法注意第一個點和最后一個點是同一個。
代碼
100ms 3MB
#include<cstdio> #include<cmath> #include<vector> #include<algorithm> using namespace std;const int maxn = 100000 + 10; int n; struct Point {double x, y;Point(double x=0, double y=0):x(x),y(y) {} }p[maxn], ch[maxn];typedef Point Vector;Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }bool operator < (const Vector& a, const Vector& b) {return a.x < b.x || (a.x == b.x && a.y < b.y); }const double eps = 1e-10; int dcmp(double x) {if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Vector& a, const Vector& b) {return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); }int ConvexHull() {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; }int main() {scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);int c = ConvexHull();double ans = 0;for(int i = 0; i < c; i++) ans += Length(ch[i]-ch[i+1]);printf("%.1lf\n", ans);return 0; }總結
以上是生活随笔為你收集整理的[codevs 1298] 凸包周长 [codevs 3201] 奶牛代理商 XI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [codevs 1743] 反转卡片
- 下一篇: [codevs 3273] 两圆的交