ACM计算几何模板 SPOJ AMR10A Playground

SPOJ AMR10A Playground

题目

O(N)

/*
 * Author: NICK WONG
 * Created Time:  7/30/2014 14:14:06
 * File Name: 
 */
#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;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=50100;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
int n,q,ask[maxn][2],x,y;
double sum,ans,f[maxn];
struct Point 
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef struct Point Vector;
Vector operator - (Point a,Point b) { return Vector(a.x-b.x,a.y-b.y);}
double Cross(Vector a, Vector b) { return a.x*b.y-a.y*b.x;}
double area(Point a, Point b, Point c)
{
    Vector u,v;
    u=a-b; v=a-c;
    double area=abs(Cross(u,v))/2;
    return area;
}
Point a[maxn];

void init()
{
    cin >> n >> q;
    for (int i=0; i<=n-1; i++)
        scanf("%lf%lf",&a[i].x,&a[i].y);
    for (int i=1; i<=q; i++)
        scanf("%d%d",&ask[i][0],&ask[i][1]);
}

void work()
{
    double one,two,three,tmp;
    memset(f,0,sizeof(f));
    for (int i=2; i<=n-1; i++)
    {
        tmp=area(a[0],a[i-1],a[i]);
        f[i]=f[i-1]+tmp;
    }
    sum=f[n-1];
    for (int i=1; i<=q; i++)
    {
        x=ask[i][0]; y=ask[i][1];
        one=f[x];
        two=area(a[0],a[x],a[y]);
        three=sum-f[y];
        ans=one+two+three;
        ans=min(ans,sum-ans);
        printf("%.1lf\n",ans);
    }
}


int main()
{
   
    init();
    work();
    return 0;
}
<table width="100%" style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224); margin-top: 10px;"><tbody><tr><td style="font-size: 13px;"><h1 style="font-size: 20px; font-weight: normal; text-align: center;">8055. Playground</h1><h2 style="font-size: 16px; font-weight: normal; text-align: center;">Problem code: AMR10A</h2></td></tr></tbody></table><br style="color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);" /><a target=_blank rel="nofollow" href="https://www.digitalocean.com/?refcode=c59674ea4847" class="freelancer_728" style="text-decoration: none; color: rgb(0, 0, 160); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);"><img src="http://www.spoj.com/gfx/digitalocean-horizontal-eps.png" width="410" height="109" border="0" alt="" /></a><span style="color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);"></span><p align="justify" style="font-size: 13px; text-align: justify; color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224);"></p><p style="font-size: 13px; text-align: justify; color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224);">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? 
 
<strong>INPUT</strong>
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. 
 
<strong>OUTPUT</strong>
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. 
 
<strong>CONSTRAINTS</strong> 
4 <= N <= 50000 
1 <= Q <= 50000 
-20,000,000 <= x,y <= 20,000,000 
0 <= a < b-1 
b < N 
 
<strong>SAMPLE INPUT</strong>
4 2 
0 0 
0 1 
1 2 
1 0 
1 3 
0 2 
 
<strong>SAMPLE OUTPUT</strong>
0.5 
0.5 
 
<strong>EXPLANATION</strong>
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.</p>



ACM 很全的计算几何模板 基础部分 1.几何公式 5 1.1三角形 5 1.2四边形 5 1.3正n边形 5 1.4圆 5 1.5棱柱 6 1.6棱锥 6 1.7棱台 6 1.8圆柱 6 1.9圆锥 6 1.10圆台 7 1.11球 7 1.12球台 7 1.13球扇形 7 2.直线与线段 7 2.0预备函数 7 2.1判三点是否共线 8 2.2判点是否在线段上 9 2.3判断两点在线段的同一侧 9 2.4判断两点是否在线段的异侧 9 2.5求点关于直线的对称点 10 2.7判断两线段是否相交 10 2.7.1常用版 10 2.7.2不常用版 11 2.8 求两条直线的交点 11 2.9点到直线的最近距离 12 2.10点到线段的最近距离 12 3.多边形 12 3.0 预备浮点函数 12 3.1判定是否是凸多边形 13 3.2判定点是否在多边形内 14 3.3 判定一条线段是否在一个任意多边形内 15 4. 三角形 16 4.0预备函数 16 4.1求三角形的外心 17 4.2求三角形内心 17 4.3求三角形垂心 17 5. 圆 18 5.0预备函数 18 5.1判定直线是否与圆相交 19 5.2判定线段与圆相交 19 5.3判圆和圆相交 19 5.4计算圆上到点p最近点 19 5.5计算直线与圆的交点 20 5.6计算两个圆的交点 20 6. 球面 21 6.0给出地球经度纬度,计算圆心角 21 6.1已知经纬度,计算地球上两点直线距离 21 6.2已知经纬度,计算地球上两点球面距离 21 7. 三维几何的若干模板 22 7.0预备函数 22 7.1判定三点是否共线 23 7.2判定四点是否共面 23 7.1判定点是否在线段上 23 7.2判断点是否在空间三角形上 24 7.3判断两点是否在线段同侧 24 7.4判断两点是否在线段异侧 25 7.5判断两点是否在平面同侧 25 7.6判断两点是否在平面异侧 25 7.7判断两空间直线是否平行 25 7.8判断两平面是否平行 26 7.9判断直线是否与平面平行 26 7.10判断两直线是否垂直 26 7.11判断两平面是否垂直 26 7.12判断两条空间线段是否相交 27 7.13判断线段是否与空间三角形相交 27 7.14计算两条直线的交点 28 7.15计算直线与平面的交点 28 7.16计算两平面的交线 29 7.17点到直线的距离 29 7.18 计算点到平面的距离 29 7.19计算直线到直线的距离 30 7.20空间两直线夹角的cos值 30 7.21两平面夹角的cos值 30 7.22直线与平面夹角sin值 31 1.最远曼哈顿距离 31 2. 最近点对 32 3. 最近点对 34 4. 最小包围圆 36 5. 求两个圆的交点 39 6. 求三角形外接圆圆心 40 7. 求凸包 42 8.凸包卡壳旋转求出所有对踵点、最远点对 44 9. 凸包+旋转卡壳求平面面积最大三角 47 10. Pick定理 50 11. 求多边形面积和重心 51 12. 判断一个简单多边形是否有核 52 13. 模拟退火 54 14. 六边形坐标系 56 15. 用一个给定半径的圆覆盖最多的点 60 16. 不等大的圆的圆弧表示 62 17. 矩形面积并 62 18. 矩形的周长并 66 19. 最近圆对 70 20. 求两个圆的面积交 74
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值