码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ AMR10A Playground(计算几何)

时间:2014-08-01 19:43:32      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   for   cti   ar   工作   

Playground

Time limit: 2s
Source limit: 50000B
Memory limit: 256MB



My kid‘s school cleared a large field on their property recently to convert it into a playing area.  The field is polygonal.  The school administration decided to separate the field into two areas by building a straight picket fence between the area for the older kids and the area for the younger kids.  The fence would go between two non-adjacent vertices of the polygonal field, and given the shape of the field, all such possible fences would lie strictly and entirely within the field. 
Naturally, the smaller of the two areas would go to the younger kids.  So can you help the school determine what the area of the smaller play-area would be for different fence positions? 
 
INPUT
The first line contains 2 numbers N denoting the number of points in the convex polygon and Q denoting the number of possible locations of straight line fences. 
The next N lines contain 2 integers each. The ith line contains the integers xi yi denoting the coordinates of the ith point of the polygon. The points are given in clockwise order. 
The next Q lines contain 2 integers a b denoting that a straight line fence is to be drawn connecting a and b. 
 
OUTPUT
Output Q lines one corresponding to each query. For each query, output the area of the smaller region for the corresponding query truncated to 1 decimal place. Always have 1 digit after the decimal place, so if the answer is 1, output it as 1.0 instead. 
 
CONSTRAINTS 
4 <= N <= 50000 
1 <= Q <= 50000 
-20,000,000 <= x,y <= 20,000,000 
0 <= a < b-1 
b < N 
 
SAMPLE INPUT
4 2 
0 0 
0 1 
1 2 
1 0 
1 3 
0 2 
 
SAMPLE OUTPUT
0.5 
0.5 
 
EXPLANATION
The polygon is given by the points (0,0) (0,1) (1,2) (1,0).  
In the first query, we join the points (0,1) and (1,0) which leads to the 2 areas given by (0,0) (0,1) (1,0) and (0,1) (1,2) (1,0). The first triangle has an area of 0.5 and the second triangle has an area of 1. The minimum of these 2 is 0.5. 
In the second query, we join the points (0,0) and (1,2) which leads to the 2 areas given by (0,0) (0,1) (1,2) and (0,0) (1,2) (1,0). The first triangle has an area of 0.5 and the second triangle has an area of 1. The minimum of these 2 is 0.5.


题意:顺时针给出构成多边形的n个点的坐标,然后进行Q次询问,每次给出两个点,意思是在这两个点之间连一条线,把多边形分成2部分,求这两部分中面积较小的那部分的面积是多少。


分析:如果每次连线之后都重新求面积,会做很多重复的工作,最终导致超时。正确解法是:利用三角形的有向面积求出多边形面积的同时,再求出面积的前缀和,当在两个点之间连一条线时,只需用前缀和相减即可。


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    double x, y;
}a[50005];
double sum[50005];
double area2(double x0, double y0, double x1, double y1, double x2, double y2)
{
    return x0 * y1 + x2 * y0 + x1 * y2 - x2 * y1 - x0 * y2 - x1 * y0;
}
int main()
{
    int n, Q, i, j;
    while(~scanf("%d%d",&n,&Q))
    {
        for(i = 0; i < n; i++)
        {
            scanf("%lf%lf",&a[i].x, &a[i].y);
        }
        double Area = 0;
        sum[0] = 0;
        for(i = 1; i <= n - 2; i++)
        {
            Area += area2(a[0].x, a[0].y, a[i].x, a[i].y, a[i+1].x, a[i+1].y);
            sum[i] = Area;
        }
        double total_area = fabs(Area / 2);
        int u, v;
        double tmp_area;
        while(Q--)
        {
            scanf("%d%d",&u, &v);
            if(u > v) swap(u, v);
            if(u == 0)
                tmp_area = fabs(sum[v-1]/2);
            else if(v == n-1)
                tmp_area = total_area - fabs(sum[u-1] + area2(a[0].x, a[0].y, a[u].x, a[u].y, a[v].x, a[v].y))/2;
            else
                tmp_area = fabs(sum[v-1] - sum[u-1] - area2(a[0].x, a[0].y, a[u].x, a[u].y, a[v].x, a[v].y))/2;
            printf("%.1lf\n", min(tmp_area, total_area - tmp_area));
        }
    }
    return 0;
}

SPOJ AMR10A Playground(计算几何),布布扣,bubuko.com

SPOJ AMR10A Playground(计算几何)

标签:style   color   os   io   for   cti   ar   工作   

原文地址:http://blog.csdn.net/lyhvoyage/article/details/38340693

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!