SPOJ AMR10A Playground(计算几何)

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(计算几何)

时间: 2024-12-23 13:41:52

SPOJ AMR10A Playground(计算几何)的相关文章

SPOJ UMR 10A 计算几何

DES:顺时针给出构成凸多边形的点.然后有Q个询问任意给出两个点的编号,询问由这两个点的连线将多边形分成的两部分面积较小的部分面积大小. 比赛时直接每次连线后求多边形求面积超时了.正确解法是求出利用叉积球三角形面积不断求和求出多边形总面积的同时,保留多边形的前缀和.当任意两点连线时,只要用前缀和想减再减去一个三角形的面积即可. 然而...我不理解的是关于q的那个循环...for就是超时...while就是AC...坐标明明是interger...int就WA...double就AC... #in

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

UESTC 2014 Summer Training #16 Div.2

虽然被刷了还是要继续战斗下去嗯...就是基础不好,难度相对较大 A.SPOJ AMR10A 点是顺时针给出的,可以在图上画画(脑补也行),连线x-a,x-b(x为选定的一个点,比如第一个点),就把所求面积分成了四部分,a-b左边部分是较容易求出来的, 三角形面积是直接可求,另外两个多边形面积是可以预处理出来的(多个三角形面积和) 反正我是沒想出來...看題解也理解半天,多邊形面積转化为三角形面积和 嗯嗯 #include <iostream> #include <cstdio> #

SPOJ CIRU The area of the union of circles (计算几何)

题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstrin

SPOJ 3273 - Order statistic set , Treap

原文  http://gashero.iteye.com/blog/2075324 目录 1   简介 2   Swift入门 3   简单值 4   控制流 5   函数与闭包 6   对象与类 7   枚举与结构 1   简介 今天凌晨Apple刚刚发布了Swift编程语言,本文从其发布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS&OSX开发有所帮助. Swift是供iOS和OS X应用编程的新编程语言,基于C和Obje

【转】[专题学习][计算几何]

原文地址:http://www.cnblogs.com/ch3656468/archive/2011/03/02/1969303.html 基本的叉积.点积和凸包等东西就不多说什么了,网上一搜一大堆,切一些题目基本熟悉了就差不多了. 一些基本的题目可以自己搜索,比如这个blog:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 接下来,研究了半平面交,思想方法看07年朱泽园的国家队论文,模板代码参考自我校大牛韬哥: http://www.o

计算几何题目分类

转载 一.基础题目 1.1 有固定算法的题目 A, 最近点对问题最近点对问题的算法基于扫描线算法.ZOJ 2107    Quoit Design    典型最近点对问题POJ    3714    Raid    变种最近点对问题 B,最小包围圆最小包围圆的算法是一种增量算法,期望是O(n).ZOJ    1450    Minimal Circle  HDU    3007    Buried memory C,旋转卡壳POJ 3608    Bridge Across Islands   

SPOJ 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

SPOJ 3273

传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 1 //SPOJ 3273 2 //by Cydiater 3 //2016.8.31 4 #include <iostream> 5 #include <cstring> 6 #include <ctime> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include