矩形面积
Accepts: 717
Submissions: 1619
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少。
Input
第一行一个正整数 T,代表测试数据组数(1≤T≤20),接下来
T 组测试数据。
每组测试数据占若干行,第一行一个正整数 N(1≤N<≤1000),代表矩形的数量。接下来
N 行,每行 8 个整数x1,y1,x2,y2,x3,y3,x4,y4,代表矩形的四个点坐标,坐标绝对值不会超过10000。
Output
对于每组测试数据,输出两行:
第一行输出"Case #i:",i 代表第 i 组测试数据。 第二行包含1 个数字,代表面积最小的矩形的面积,结果保留到整数位。
Sample Input
2 2 5 10 5 8 3 10 3 8 8 8 8 6 7 8 7 6 1 0 0 2 2 2 0 0 2
Sample Output
Case #1: 17 Case #2: 4
ac代码
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; #define PR 1e-8 #define N 4010 #define maxdouble 1e20 struct TPoint { double x,y; }; struct TPolygon { int n; TPoint p[N]; }ply; double MIN(double a,double b) {return a>b?b:a;} int dblcmp(double a) { if(fabs(a)<PR) return 0; return a>0?1:-1; } double dist(TPoint a,TPoint b) { double s1=a.x-b.x; double t1=a.y-b.y; return sqrt(s1*s1+t1*t1); } double cross(TPoint a,TPoint b,TPoint c) { double s1=b.x-a.x; double t1=b.y-a.y; double s2=c.x-a.x; double t2=c.y-a.y; return s1*t2-s2*t1; } double dot(TPoint a,TPoint b,TPoint c) { double s1=b.x-a.x; double t1=b.y-a.y; double s2=c.x-a.x; double t2=c.y-a.y; return s1*s2+t1*t2; } bool cmop(TPoint a,TPoint b) { if(fabs(a.x-b.x)<PR) return a.y<b.y; else return a.x<b.x; } bool cmp(TPoint a,TPoint b) { int d1=dblcmp(cross(ply.p[0],a,b)); return d1>0||(d1==0&&dist(ply.p[0],a)<dist(ply.p[0],b)); } TPolygon graham() { int i,top=2; for(i=2;i<ply.n;i++) { while(top>1&&(dblcmp(cross(ply.p[top-2],ply.p[i],ply.p[top-1])))>=0) top--; ply.p[top++]=ply.p[i]; } ply.n=top; return ply; } double solve() { int i,p=1,q=1,r; double minarea=maxdouble,area; ply.p[ply.n]=ply.p[0]; for(i=0;i<ply.n;i++) { while(dblcmp(cross(ply.p[i],ply.p[i+1],ply.p[p+1])-cross(ply.p[i],ply.p[i+1],ply.p[p]))>0) p=(p+1)%ply.n; while(dblcmp(dot(ply.p[i],ply.p[i+1],ply.p[q+1])-dot(ply.p[i],ply.p[i+1],ply.p[q]))>0) q=(q+1)%ply.n; if(i==0) r=q; while(dblcmp(dot(ply.p[i],ply.p[i+1],ply.p[r+1])-dot(ply.p[i],ply.p[i+1],ply.p[r]))<=0) r=(r+1)%ply.n; double d=dist(ply.p[i],ply.p[i+1])*dist(ply.p[i],ply.p[i+1]); area=cross(ply.p[i],ply.p[i+1],ply.p[p])*(dot(ply.p[i],ply.p[i+1],ply.p[q])-dot(ply.p[i],ply.p[i+1],ply.p[r]))/d; minarea=MIN(area,minarea); } return minarea; } int main() { int t,cot=0; scanf("%d",&t); while(t--) { int i,n; double area; scanf("%d",&n); ply.n=n*4; for(i=0;i<ply.n;i++) scanf("%lf%lf",&ply.p[i].x,&ply.p[i].y); sort(ply.p,ply.p+ply.n,cmop); sort(ply.p+1,ply.p+ply.n,cmp); ply=graham(); if(ply.n<3) area=0; else area=solve(); printf("Case #%d:\n",++cot); printf("%.lf\n",area); } return 0; }
时间: 2024-10-18 18:14:14