【Codeforces Round 339 (Div 2)C】【计算几何 点到直线的距离模板】Peter and Snow Blower 多边形整体绕一圆心旋转的运行面积
2016-03-08 19:20
645 查看
C. Peter and Snow Blower
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point
that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point
lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and
now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (
![](http://codeforces.com/predownloaded/65/fb/65fb82ac413eedfe96bf1c3c34b10f103528c70a.png)
),
and coordinates of point P.
Each of the next n lines
contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in
their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a,
and the answer of the jury is b. The checker program will consider your answer correct, if
![](http://codeforces.com/predownloaded/c6/2e/c62ea64d4651240724c5ac4779b671c741edec24.png)
.
Examples
input
output
input
output
Note
In the first sample snow will be removed from that area:
![](http://codeforces.com/predownloaded/e8/8a/e88a2125fe659076b3f0f50e7384a14fd8e813b0.png)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const double PI = acos(-1.0);
int n;
//做与二维点相关的题,最好要用写Point结构体的好习惯
struct Point
{
LL x, y;
Point() {}
Point(int x_, int y_) { x = x_; y = y_; }
Point operator - (const Point& b)const
{
return Point(x - b.x, y - b.y);
}
}P,a
;
//求两个向量的点积
double Dot(Point& a,Point& b)
{
return a.x*b.x + a.y*b.y;
}
//返回一个向量的长度
double Len(Point a)
{
return sqrt(a.x*a.x + a.y*a.y);
}
//返回两个点之间的距离
double Dis(Point& a,Point& b)
{
return Len(Point(a - b));
}
//返回点a到线段bc的最短距离
double Point_To_Segment(Point& a,Point& b,Point& c)
{
Point x = a - b;
Point y = c - b;
Point z = c - a;
if (Dot(x, y) < 0)return Len(x);
if (Dot(y, z) < 0)return Len(z);
return fabs((x.x*y.y - x.y*y.x) / Len(y));
}
int main()
{
while (~scanf("%d%lld%lld", &n,&P.x,&P.y))
{
double mindis=1e18;
double maxdis=0;
for (int i = 1; i <= n; ++i)scanf("%lld%lld", &a[i].x, &a[i].y);
a[n + 1] = a[1];
for (int i = 1; i <= n;++i)
{
gmax(maxdis, Dis(P, a[i]));
gmin(mindis, Point_To_Segment(P, a[i], a[i + 1]));
}
double ans = PI*(maxdis*maxdis - mindis*mindis);
printf("%.15f\n", ans);
}
return 0;
}
/*
【trick&&吐槽】
1,这题真的要骂自己是傻逼= =,我三分了,用的却不是double存结果。
导致最后输出的必然是个整数,于是肯定错。
三分是可以正确AC的,而且三分50次就能过>_<
2,要开始学点计算几何基础了。比如点到直线的距离。
【题意】
给你一个凸包,任意三点不共线。
这个凸包围绕一个中心旋转。
问你旋转出来的面积是多少
【类型】
计算几何 点到直线距离
【分析】
两点式如何转化为一般式?
(y-y1)/(y2-y1)==(x-x1)(x2-x1)
(y-y1)(x2-x1)==(x-x1)(y2-y1)
yx2 -y1x2 -x1y +x1y1 == xy2 -x1y2 -y1x +x1y1
(y2-y1)x +(x1-x2)y == x1y2-x2y1
得到
a=(y2-y1)
b=(x1-x2)
c=x2y1-x1y2
点到直线的距离=|ax+by+c|/sqrt(a^2+b^2)
【时间复杂度&&优化】
三分:O(n*三分次数)
公式:O(n)
【数据】
3 0 0
2 2
-2 2
0 4
*/
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point
that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point
lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and
now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (
![](http://codeforces.com/predownloaded/65/fb/65fb82ac413eedfe96bf1c3c34b10f103528c70a.png)
),
and coordinates of point P.
Each of the next n lines
contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in
their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a,
and the answer of the jury is b. The checker program will consider your answer correct, if
![](http://codeforces.com/predownloaded/c6/2e/c62ea64d4651240724c5ac4779b671c741edec24.png)
.
Examples
input
3 0 0 0 1 -1 2 1 2
output
12.566370614359172464
input
4 1 -1 0 0 1 2 2 0 1 1
output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
![](http://codeforces.com/predownloaded/e8/8a/e88a2125fe659076b3f0f50e7384a14fd8e813b0.png)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const double PI = acos(-1.0);
int n;
//做与二维点相关的题,最好要用写Point结构体的好习惯
struct Point
{
LL x, y;
Point() {}
Point(int x_, int y_) { x = x_; y = y_; }
Point operator - (const Point& b)const
{
return Point(x - b.x, y - b.y);
}
}P,a
;
//求两个向量的点积
double Dot(Point& a,Point& b)
{
return a.x*b.x + a.y*b.y;
}
//返回一个向量的长度
double Len(Point a)
{
return sqrt(a.x*a.x + a.y*a.y);
}
//返回两个点之间的距离
double Dis(Point& a,Point& b)
{
return Len(Point(a - b));
}
//返回点a到线段bc的最短距离
double Point_To_Segment(Point& a,Point& b,Point& c)
{
Point x = a - b;
Point y = c - b;
Point z = c - a;
if (Dot(x, y) < 0)return Len(x);
if (Dot(y, z) < 0)return Len(z);
return fabs((x.x*y.y - x.y*y.x) / Len(y));
}
int main()
{
while (~scanf("%d%lld%lld", &n,&P.x,&P.y))
{
double mindis=1e18;
double maxdis=0;
for (int i = 1; i <= n; ++i)scanf("%lld%lld", &a[i].x, &a[i].y);
a[n + 1] = a[1];
for (int i = 1; i <= n;++i)
{
gmax(maxdis, Dis(P, a[i]));
gmin(mindis, Point_To_Segment(P, a[i], a[i + 1]));
}
double ans = PI*(maxdis*maxdis - mindis*mindis);
printf("%.15f\n", ans);
}
return 0;
}
/*
【trick&&吐槽】
1,这题真的要骂自己是傻逼= =,我三分了,用的却不是double存结果。
导致最后输出的必然是个整数,于是肯定错。
三分是可以正确AC的,而且三分50次就能过>_<
2,要开始学点计算几何基础了。比如点到直线的距离。
【题意】
给你一个凸包,任意三点不共线。
这个凸包围绕一个中心旋转。
问你旋转出来的面积是多少
【类型】
计算几何 点到直线距离
【分析】
两点式如何转化为一般式?
(y-y1)/(y2-y1)==(x-x1)(x2-x1)
(y-y1)(x2-x1)==(x-x1)(y2-y1)
yx2 -y1x2 -x1y +x1y1 == xy2 -x1y2 -y1x +x1y1
(y2-y1)x +(x1-x2)y == x1y2-x2y1
得到
a=(y2-y1)
b=(x1-x2)
c=x2y1-x1y2
点到直线的距离=|ax+by+c|/sqrt(a^2+b^2)
【时间复杂度&&优化】
三分:O(n*三分次数)
公式:O(n)
【数据】
3 0 0
2 2
-2 2
0 4
*/
相关文章推荐
- 设计模式之行为型模式 - 调用行为的传递问题
- [div+css]晒晒最新制作专题推广页模板
- 2008大学生入党申请书 模板
- IMAIL多语言模板两套Outlook&Gmail模板下载
- 在PHP中使用模板的方法
- 深入解析php模板技术原理【一】
- Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
- 在ASP中不用模板生成HTML静态页直接生成.html页面
- C#模板方法模式(Template Method Pattern)实例教程
- javascript文本模板用法实例
- 关于Asp代码与页面的分离模板技术第1/3页
- php模板原理讲解
- 需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
- DataGrid 动态添加模板列 实现代码
- 详解java模板和回调机制
- C++模板之特化与偏特化详解
- vs.net2008添加模板方法
- ThinkPHP模板判断输出Empty标签用法详解
- ThinkPHP模板范围判断输出In标签与Range标签用法详解
- thinkphp模板用法和内容输出实例