Hdu 3685 Rotational Painting(多边形重心+凸包)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3685

思路:先求出多边形重心,放置的边一定为凸包边。判断重心是否落在边之间(求点到直线与点到线段的距离,判断)。

4

0 0

4 0

8 4

4 4

注意这种情况,重心不能在凸包边端点的垂线上。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int maxn=5e4+50;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
};
Point operator - (const Point &A,const Point &B)
{
    return Point(A.x-B.x,A.y-B.y);
}
bool operator < (const Point &a,const Point &b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return (x<0?-1:1);
}
bool operator == (const Point &a,const Point &b)
{
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(const Point &A,const Point &B)
{
    return A.x*B.x+A.y*B.y;
}
double Cross(Point A, Point B)
{
    return A.x*B.y-A.y*B.x;
}
double Length(const Point &A)
{
    return sqrt(Dot(A,A));
}
double Area(Point A,Point B,Point C)
{
    return Cross(B-A, C-A)/2.0;
}
Point centre_of_gravity(Point *p, int n)
{
    Point G;
    double s,sumS=0;
    G.x=0,G.y=0;
    for(int i=1; i<n-1; i++)
    {
        s=Area(p[0], p[i], p[i+1]);
        sumS+=s;
        G.x+=(p[0].x+p[i].x+p[i+1].x)*s;
        G.y+=(p[0].y+p[i].y+p[i+1].y)*s;
    }
    G.x=G.x/sumS/3.0;
    G.y=G.y/sumS/3.0;
    return G;
}
int convex_hull(Point *p,Point *ch,int n)
{
    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-1])<=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-1])<=0)
            m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}
double DistanceToLine(const Point &P,const Point &A,const Point &B)
{
    Point v1=B-A,v2=P-A;
    return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(const Point &P,const Point &A,const Point &B)
{
    if(A==B) return Length(P-A);
    Point v1=B-A,v2=P-A,v3=P-B;
    if(dcmp(Dot(v1,v2)<0)) return Length(v2);
    else if(dcmp(Dot(v1,v3))>0) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
int n;
Point P[maxn],hull[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            double x,y;
            scanf("%lf%lf",&x,&y);
            P[i]=Point(x,y);
        }
        int ans=0;
        Point G=centre_of_gravity(P,n);
       // cout<<G.x<<" "<<G.y<<endl;
        int num=convex_hull(P,hull,n);
        //cout<<num<<endl;
        /*for(int i=0;i<num;i++)
            cout<<hull[i].x<<" "<<hull[i].y<<endl;*/
        for(int i=0; i<num; i++)
        {
            int j=(i+1)%num;
            if (dcmp (Dot (G-hull[i], hull[j]-hull[i])) == 0 || dcmp (Dot (G-hull[j], hull[i]-hull[j])) == 0) continue;
            double DS=DistanceToSegment(G,hull[i],hull[j]);
            double DL=DistanceToLine(G,hull[i],hull[j]);
            //cout<<DS<<" "<<DL<<endl;
            if(dcmp(DS-DL)==0) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-12 17:06:26

Hdu 3685 Rotational Painting(多边形重心+凸包)的相关文章

hdu 3685 多边形重心+凸包

Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2498    Accepted Submission(s): 702 Problem Description Josh Lyman is a gifted painter. One of his great works is a glass pain

hdu 1115(计算多边形重心)

题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi 特殊地,若每个点的质量相同,则 X = ∑xi / n Y = ∑yi / n 2,质量分布均匀.这个题就是这一类型,算法和上面的不同. 特殊地,质量均匀的三角形重心: X = ( x0 + x1 + x2 ) / 3

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

hdu 3685 10 杭州 现场 F - Rotational Painting 重心

F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3685 Appoint description:  System Crawler  (2014-11-09) Description Josh Lyman is a gifted painter. One of his great works

hdu 1115(多边形重心问题)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6971    Accepted Submission(s): 2919 Problem Description There are many secret openings in the floor which are covered by a big

HDU 1115(求质量均匀分布的多边形重心 物理)

题意是给一个 n 边形,给出沿逆时针方向分布的各顶点的坐标,求出 n 边形的重心. 求多边形重心的情况大致上有三种: 一.多边形的质量都分布在各顶点上,像是用轻杆连接成的多边形框,各顶点的坐标为Xi,Yi,质量为mi,则重心坐标为: X = ∑( xi * mi ) /  ∑ mi ; Y = ∑( yi * mi)  / ∑ mi; 若每个顶点的质量相等,则重心坐标为: X = ∑ xi / n; Y = ∑ yi / n; 二.多边形的质量分布均匀,像是用密度相同的材料制成的多边形板子,多采

hdu1115(多边形重心算法)

题目意思: 给出一个n边形的n个顶点,求出这个n边形的重心坐标. http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目分析: /** *出处:http://blog.csdn.net/ysc504/article/details/8812339 *①质量集中在顶点上 *  n个顶点坐标为(xi,yi),质量为mi,则重心 * X = ∑( xi×mi ) / ∑mi * Y = ∑( yi×mi ) / ∑mi * 特殊地,若每个点的质量相同,则 *

多边形重心模板

HDU 1115 Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5719    Accepted Submission(s): 2391 Problem Description There are many secret openings in the floor which are covered

UVALive 4426 Blast the Enemy! --求多边形重心

题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define Mod 100000000