日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[codevs 1298] 凸包周长 [codevs 3201] 奶牛代理商 XI

發布時間:2025/3/15 编程问答 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。