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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ACM-ICPC(国际大学生程序设计竞赛)是一项面向大学生的计算机编程竞赛,涉及算法和数据结构等领域。在比赛中,选手需要解决一系列编程问题,使用合适的算法和数据结构来实现正确和高效的解决方案。 对于整理ACM-ICPC模板,以下是一些建议: 1. 了解比赛要求:首先,你需要了解ACM-ICPC比赛的具体要求和规则。这包括了解比赛所涉及的算法和数据结构,以及题目的类型和难度等。 2. 收集资料:收集与ACM-ICPC相关的资料,包括经典算法和数据结构的实现代码、常见问题的解题思路等。可以参考教材、博客、论文等资源。 3. 整理模板:将收集到的资料整理成模板。可以按照算法和数据结构的分类进行整理,例如排序算法、图算法、字符串算法等。对每个模板,添加必要的注释和示例代码,以便理解和使用。 4. 测试代码:对每个模板编写测试代码,确保它们的正确性和可靠性。可以使用已知的测试用例或自行设计测试用例。 5. 更新与扩充:定期更新和扩充模板,以适应ACM-ICPC比赛中新出现的算法和数据结构。同时,根据自己的经验和理解,对模板进行优化和改进。 6. 练习和复习:在比赛之前,利用整理好的模板进行练习和复习。尝试解决一些经典问题,使用模板中的算法和数据结构进行实现,并进行优化。 希望这些建议对你整理ACM-ICPC模板有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值