hdu 5251(2015百度之星初赛+旋转卡壳)

来源:互联网 发布:中国网络实名制喷子 编辑:程序博客网 时间:2024/05/22 16:38

题目链接:点击打开链接;

题意:

小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少。
分析: 把矩形的四个个点列出来,然后求解出凸包,接下来就是求解凸包的最小矩形覆盖,很明显是旋转卡壳做法,关于旋转卡壳详见:
旋转卡壳:点击打开链接   (转载出处 http://www.cnblogs.com/Booble/)
本题代码如下:
#include <map>#include <set>#include <vector>#include <math.h>#include <string>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>#include <functional>using namespace std;const int MAXN=1005;const double eps=1e-10;int dcmp(double x){    if(fabs(x)<eps)return 0;    if(x>0)return 1;    return -1;}                                                   //弄精度struct Point {    double x,y;}p[MAXN];                                           //搞点double dot(Point a,Point b,Point 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;}                                                     //点积int n,res[MAXN],top;bool cmp(Point a,Point b){    if(a.y==b.y)return a.x<b.x;    return a.y<b.y;}bool mult(Point sp,Point ep,Point op){    return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);}void Graham(){    int len;    top=1;    sort(p,p+n,cmp);    if(n==0)return;res[0]=0;    if(n==1)return;res[1]=1;    if(n==2)return;res[2]=2;    for(int i=2;i<n;i++){        while(top&&mult(p[i],p[res[top]],p[res[top-1]]))top--;        res[++top]=i;    }    len=top;    res[++top]=n-2;    for(int i=n-3;i>=0;i--){        while(top!=len&&mult(p[i],p[res[top]],p[res[top-1]]))top--;        res[++top]=i;    }}                                                               //求凸包double dist(Point a,Point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double Cross(Point a,Point b,Point c){    return (c.x-a.x)*(b.y-a.y) - (b.x-a.x)*(c.y-a.y);}double ComGeo(){    int R=1,U=1,L;    double ans=99999999999999;    for(int i=0;i<top;i++){        while(dcmp(fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[(U+1)%top]]))-fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[U]])))>=0) U=(U+1)%top;//最上面一点        while(dcmp(dot(p[res[i]],p[res[(i+1)%top]],p[res[(R+1)%top]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[R]]))>0) R=(R+1)%top;//最右一点        if(i==0)L=R;        while(dcmp(dot(p[res[i]],p[res[(i+1)%top]],p[res[(L+1)%top]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[L]]))<=0) L=((L+1)%top)%top;//最左一点        //旋转卡壳精髓所在        double d=dist(p[res[i]],p[res[(i+1)%top]])*dist(p[res[i]],p[res[(i+1)%top]]);        double area=fabs(Cross(p[res[i]],p[res[(i+1)%top]],p[res[U]]))*//求面积        fabs(dot(p[res[i]],p[res[(i+1)%top]],p[res[R]])-dot(p[res[i]],p[res[(i+1)%top]],p[res[L]]))/d;        if(area<ans) ans = area;    }    return ans;}                                               //旋转卡壳int main(){    int t;    scanf("%d",&t);    int k=1;    while(t--){        int m;        scanf("%d",&m);        n=0;        double x;        for(int i=0;i<m;i++){            for(int j=1;j<=8;j++){                scanf("%lf",&x);                if(j&1)p[n].x=x;                else p[n++].y=x;            }        }        Graham();        double ans;        if(top<3)ans=0;        else        ans=ComGeo();        long long sum=ans+0.5;        printf("Case #%d:\n%I64d\n",k++,sum);    //最后不要忘记四舍五入    }    return 0;}


0 0
原创粉丝点击