HIT暑期集训 计算几何基础

网上找的一个计算几何的总结博客:https://blog.csdn.net/clover_hxy/article/details/53966405

一个关于计算几何模板的博客:https://blog.csdn.net/clasky/article/details/9990235

C    POJ 2398

将隔板从左至右排序,二分隔板来找玩具落在哪两个隔板中间(叉乘判断玩具落在隔板的哪个方向),数组统计每个格子中有几个玩具,最后再for一边统计答案

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 1005
using namespace std;
int num[maxn],ans[maxn];
struct node
{
    double x,y;
}p;
struct line
{
    node s,t;
}e[maxn];
bool cmp(line n,line m){return n.s.x<m.s.x;}
bool jud(node a,node b,node c)
{
    node ab,ac;
    ab.x=a.x-b.x;
    ab.y=a.y-b.y;
    ac.x=a.x-c.x;
    ac.y=a.y-c.y;
    double ss=ab.x*ac.y-ab.y*ac.x;
    if (ss<0) return 0;
    return 1;
}
int main()
{
    int i,n,m,l,r,mid,tmp;
    double x1,y1,x2,y2;
    while (scanf("%d",&n)!=EOF && n!=0)
    {
        memset(num,0,sizeof(num));
        memset(ans,0,sizeof(ans));
        scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
        p.x=x1;
        p.y=y1;e[0].s=p;
        p.y=y2;e[0].t=p;
        n++;
        p.x=x2;
        p.y=y1;e[n].s=p;
        p.y=y2;e[n].t=p;
        for (i=1;i<n;i++) 
        {
            scanf("%lf",&p.x);
            p.y=y1;
            e[i].s=p;
            scanf("%lf",&p.x);
            p.y=y2;
            e[i].t=p;
        }
        sort(e+1,e+n+1,cmp);
        while (m--)
        {
            scanf("%lf%lf",&p.x,&p.y);
            l=1;r=n+1;
            while (l<=r)
            {
                mid=(l+r)>>1;
                tmp=jud(e[mid].s,e[mid].t,p);
                if (tmp) l=mid+1;
                else r=mid-1;
            }
            num[l]++;
        }
        for (i=1;i<=n+1;i++)
            if (num[i]) ans[num[i]]++;
        printf("Box
");
        for (i=1;i<=n;i++) 
            if (ans[i]) printf("%d: %d
",i,ans[i]);
    }
    return 0;
}
View Code

E    HihoCoder 1879

F    Kattis oil2

 题解待补

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 4005
#define eps 1e-7
using namespace std;
int f[maxn];
struct in
{
    int id;
    double k;
}a[maxn];
struct node
{
    double x[2],y;
}e[maxn];
bool cmp(in x,in y){return x.k<y.k;}
int main()
{
    int i,j,k,n,cnt,id;
    double x,y,now,ans;
    while (scanf("%d",&n)!=EOF)
    {
        ans=0;
        for (i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&e[i].x[0],&e[i].x[1],&e[i].y);
            if (e[i].x[0]>e[i].x[1]) swap(e[i].x[0],e[i].x[1]);
            e[i].x[0]-=eps;e[i].x[1]+=eps;
            
        }
        for (i=1;i<=n;i++)
        for (k=0;k<=1;k++)
        {
            x=e[i].x[k];
            y=e[i].y;
            cnt=0;now=e[i].x[1]-e[i].x[0];
            if (now>ans) ans=now;
            for (j=1;j<=n;j++)
            {
                if (e[j].y==y) continue;
                a[++cnt].k=(x-e[j].x[0])/(y-e[j].y);a[cnt].id=j;
                a[++cnt].k=(x-e[j].x[1])/(y-e[j].y);a[cnt].id=j;
            }
            sort(a+1,a+cnt+1,cmp);
            for (j=1;j<=cnt;j++)
            {
                id=a[j].id;
                if (!f[id]) now+=e[id].x[1]-e[id].x[0];
                else now-=e[id].x[1]-e[id].x[0];
                f[id]^=1;
                if (now>ans) ans=now;
            }
            
        }
        printf("%.0lf
",ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/lsykk/p/13521863.html