SPOJ AMR 10A Playground 计算几何

题目大意:

给出一个多边形的顶点数N,给出接下来的询问数Q,接下来是N个顶点的坐标,按顺时针顺序给出,顶点一次编号为0到N - 1

现在给出Q组询问,每组两个数 x, y代表将顶点编号为 x 和编号为 y 的连接起来,会将原来的多边形变成两个部分,输出两个部分中较小的那个部分的面积


大致思路:

首先由于题目当中多边形的顶点最多会有50000个,然后询问次数也可以达到50000个,如果对于每一种连接都去算得到的多边形的面积,显然是会超时的

那么我们可以不用每次去算一个多边形的面积,由于每组数据输入时按顺时针输入,可以用一个数组 f [ i ] 记录由以编号为 0 到 i 的点为顶点的多边形的面积。

那么每次询问时 x ,y 时, f [ y ] - f [ x ]代表的便是由编号为从 x 到 y 的顶点和编号为 0 的点组成的多边形的面积,这样只需要算由 编号为0, x, y 组成的三角形的面积,将其减去即可

这样子每次询问所需要的计算量大大降低


代码如下:

Result  :  Accepted     Memory  :  3789 KB     Time  :  1470 ms

/*
 * Author: Gatevin
 * Created Time:  2014/7/30 14:22:36
 * File Name: 123.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 50010

int cmp(double x)
{
    return x < -eps ? - 1 : ( x > eps ? 1 : 0);
}

const double pi = acos(-1.0);

inline double sqr(double x)
{
    return x*x;
}

struct point
{
    double x, y;
    point(){}
    point(double a, double b) : x(a), y(b) {}
    friend point operator + (const point & a, const point & b)
    {
        return point(a.x + b.x, a.y + b.y);
    }
    friend point operator - (const point & a, const point & b)
    {
        return point(a.x - b.x, a.y - b.y);
    }
    double norm()
    {
        return sqrt(sqr(x) + sqr(y));
    }
};

double det(const point & a, const point & b)
{
    return a.x * b.y - a.y * b.x;
}

struct triangle
{
    point a[4];
    triangle(){}
    double area()
    {
        double sum = 0;
        a[3] = a[0];
        for(int i = 0; i < 3; i++)
        {
            sum += det(a[i + 1], a[i]);
        }
        return sum/2;
    }
};

int N,Q;
point all[maxn];
double f[maxn];

int main()
{
    scanf("%d %d", &N, &Q);
    f[0] = 0;
    f[1] = 0;
    double tmp1,tmp2;
    triangle tri;
    for(int i = 0; i < N; i++)
    {
        scanf("%lf%lf", &tmp1, &tmp2);
        all[i] = point(tmp1, tmp2);
    }
    tri.a[0] = all[0];
    for(int i = 2; i <= N - 1; i++)
    {
        tri.a[1] = all[i - 1];
        tri.a[2] = all[i];
        f[i] = tri.area() + f[i - 1];
    }
    double sum = f[N - 1];
    int tmp3,tmp4;
    triangle two;
    two.a[0] = all[0];
    while(Q--)
    {
        scanf("%d %d", &tmp3, &tmp4);
        two.a[1] = all[tmp3];
        two.a[2] = all[tmp4];
        double tmp = f[tmp4] - f[tmp3] - two.area();
        if(cmp(sum - 2*tmp) == 1)
        {
            printf("%.1lf\n", tmp);
        }
        else
        {
            printf("%.1lf\n", sum - tmp);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值