首页 > 代码库 > 第二周 9.5 --- 9.11
第二周 9.5 --- 9.11
新的一周>.<
9.5
cf 340 b B - Maximal Area Quadrilateral
给出 n 个点,求能够形成的四边形的最大面积
最开始的做法是 暴力枚举4个点,再去算面积,不过不对..应该是算面积那里不对
然后应该是枚举四边形的对角线
维护对角线两边的三角形的最大值
1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <vector> 5 using namespace std; 6 7 const int INF = (1<<30)-1; 8 9 //lrj计算几何模板 10 struct Point 11 { 12 int x, y; 13 Point(double x=0, double y=0) :x(x),y(y) {} 14 }; 15 typedef Point Vector; 16 17 Point read_point(void) 18 { 19 double x, y; 20 scanf("%lf%lf", &x, &y); 21 return Point(x, y); 22 } 23 24 const double EPS = 1e-10; 25 26 //向量+向量=向量 点+向量=点 27 Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } 28 29 //向量-向量=向量 点-点=向量 30 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } 31 32 //向量*数=向量 33 Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } 34 35 //向量/数=向量 36 Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } 37 38 bool operator < (const Point& a, const Point& b) 39 { return a.x < b.x || (a.x == b.x && a.y < b.y); } 40 41 int dcmp(double x) 42 { if(fabs(x) < EPS) return 0; else return x < 0 ? -1 : 1; } 43 44 bool operator == (const Point& a, const Point& b) 45 { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } 46 47 /**********************基本运算**********************/ 48 49 //点积 50 double Dot(Vector A, Vector B) 51 { return A.x*B.x + A.y*B.y; } 52 //向量的模 53 double Length(Vector A) { return sqrt(Dot(A, A)); } 54 55 //向量的夹角,返回值为弧度 56 double Angle(Vector A, Vector B) 57 { return acos(Dot(A, B) / Length(A) / Length(B)); } 58 59 //叉积 60 double Cross(Vector A, Vector B) 61 { return A.x*B.y - A.y*B.x; } 62 63 //向量AB叉乘AC的有向面积 64 double Area2(Point A, Point B, Point C) 65 { return Cross(B-A, C-A); } 66 67 //向量A旋转rad弧度 68 Vector VRotate(Vector A, double rad) 69 { 70 return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad)); 71 } 72 73 //将B点绕A点旋转rad弧度 74 Point PRotate(Point A, Point B, double rad) 75 { 76 return A + VRotate(B-A, rad); 77 } 78 79 //求向量A向左旋转90°的单位法向量,调用前确保A不是零向量 80 Vector Normal(Vector A) 81 { 82 double l = Length(A); 83 return Vector(-A.y/l, A.x/l); 84 } 85 86 /**********************点和直线**********************/ 87 88 //求直线P + tv 和 Q + tw的交点,调用前要确保两条直线有唯一交点 89 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) 90 { 91 Vector u = P - Q; 92 double t = Cross(w, u) / Cross(v, w); 93 return P + v*t; 94 }//在精度要求极高的情况下,可以自定义分数类 95 96 //P点到直线AB的距离 97 double DistanceToLine(Point P, Point A, Point B) 98 { 99 Vector v1 = B - A, v2 = P - A;100 return fabs(Cross(v1, v2)) / Length(v1); //不加绝对值是有向距离101 }102 103 //点到线段的距离104 double DistanceToSegment(Point P, Point A, Point B)105 {106 if(A == B) return Length(P - A);107 Vector v1 = B - A, v2 = P - A, v3 = P - B;108 if(dcmp(Dot(v1, v2)) < 0) return Length(v2);109 else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);110 else return fabs(Cross(v1, v2)) / Length(v1);111 }112 113 //点在直线上的射影114 Point GetLineProjection(Point P, Point A, Point B)115 {116 Vector v = B - A;117 return A + v * (Dot(v, P - A) / Dot(v, v));118 }119 120 //线段“规范”相交判定121 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)122 {123 double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);124 double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);125 return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;126 }127 128 //判断点是否在线段上129 bool OnSegment(Point P, Point a1, Point a2)130 {131 Vector v1 = a1 - P, v2 = a2 - P;132 return dcmp(Cross(v1, v2)) == 0 && dcmp(Dot(v1, v2)) < 0;133 }134 135 //求多边形面积136 double PolygonArea(Point* P, int n)137 {138 double ans = 0.0;139 for(int i = 1; i < n - 1; ++i){140 double tmp = Cross(P[i]-P[0], P[i+1]-P[0]);141 tmp = fabs(tmp);142 //printf("tmp = %lf\n",tmp);143 ans += tmp;144 }145 return ans/2;146 }147 148 Point p[5],a[505];149 int n;150 151 void solve(){152 double ans = 0.0;153 for(int i = 1;i <= n;i++){154 for(int j = i+1;j <= n;j++){155 double minn = -1.0*INF;156 double maxx = -1.0*INF;157 for(int k = 1;k <= n;k++){158 if(k == i || k == j) continue;159 // printf("i = %d j = %d k = %d ",i,j,k);160 double tmp = Cross(a[i]-a[j], a[k]-a[j]);161 if(tmp < 0) {162 tmp = -tmp;163 minn = max(tmp,minn);164 }165 else maxx = max(maxx,tmp);166 }167 ans = max(ans,(minn+maxx)/2.0);168 }169 }170 printf("%.12lf\n",ans);171 }172 173 int main(){174 while(scanf("%d",&n) != EOF){175 for(int i = 1;i <= n;i++){176 scanf("%d %d",&a[i].x,&a[i].y);177 }178 solve();179 }180 return 0;181 }
第二周 9.5 --- 9.11
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。