【模板】最小正方形覆盖(坐标旋转加三分)
作者:互联网
题目链接:https://vjudge.net/problem/POJ-3301
代码:
1 /************************************************************************* 2 > File Name: poj3301.cpp 3 # File Name: poj3301.cpp 4 # Author : xiaobuxie 5 # QQ : 760427180 6 # Email:760427180@qq.com 7 # Created Time: 2019年10月23日 星期三 19时34分28秒 8 ************************************************************************/ 9 10 #include<iostream> 11 #include<cstdio> 12 #include<map> 13 #include<cmath> 14 #include<cstring> 15 #include<set> 16 #include<queue> 17 #include<vector> 18 #include<algorithm> 19 using namespace std; 20 typedef long long ll; 21 #define inf 0x3f3f3f3f 22 #define eps 1e-8 23 const double pi = acos(-1.0); 24 int sgn(double x){ 25 if(fabs(x) < eps) return 0; 26 if(x<0) return -1; 27 return 1; 28 } 29 const int N = 49; 30 struct Point{ 31 double x,y; 32 Point(){x=y=0;} 33 Point(double a,double b){x=a,y=b;} 34 }p[N]; 35 int n; 36 double cal(double u){ 37 double maxx,minx,maxy,miny; 38 maxx = maxy = -inf; 39 minx = miny = inf; 40 for(int i = 1;i<=n;++i){ 41 double x = p[i].x*cos(u) - p[i].y*sin(u); 42 double y = p[i].x*sin(u) + p[i].y*cos(u); 43 maxx = max(maxx,x); 44 maxy = max(maxy,y); 45 minx = min(minx,x); 46 miny = min(miny,y); 47 } 48 return max( maxx - minx,maxy - miny); 49 } 50 int main(){ 51 int T; scanf("%d",&T); 52 while(T--){ 53 scanf("%d",&n); 54 for(int i = 1;i <= n; ++i) scanf("%lf %lf",&p[i].x,&p[i].y); 55 double l = 0,r = pi/2; 56 //三分旋转角度 57 while( r - l > eps){ 58 double mid1 = (l+r)/2.0; 59 double mid2 = (mid1 + r)/2.0; 60 if( cal(mid1) > cal(mid2) ) l = mid1; 61 else r = mid2; 62 } 63 double ans = cal(l); 64 printf("%.2f\n",ans*ans); 65 } 66 return 0; 67 }View Code
标签:double,ans,正方形,坐标,cal,include,mid2,模板,mid1 来源: https://www.cnblogs.com/xiaobuxie/p/11728385.html