Codeforces Round #339 (Div. 1) A. Peter and Snow Blower(點到線段距離)

題目鏈接

https://codeforces.com/contest/613/problem/A

思路

求多邊形繞一定點掃過的圓環面積。

外圓半徑r2爲多邊形頂點到定點的最長距離。內圓半徑r1爲多邊形的邊到定點的最短距離。

代碼

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=1e5+10;
int inf=1e9;
void read(int &x){
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
struct node{
	double x,y;
};
node p[maxn],s[maxn],st;

//求邊長 
double bian(node a,node b){
    return (a.x-b.x)*(a.x-b.x)+(b.y-a.y)*(b.y-a.y);
}
int n;
double PointToSegDist(double x, double y, double x1, double y1, double x2, double y2){
	double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
	if (cross <= 0) return (x - x1) * (x - x1) + (y - y1) * (y - y1);
	double d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
	if (cross >= d2) return (x - x2) * (x - x2) + (y - y2) * (y - y2);
	double r = cross / d2;
	double px = x1 + (x2 - x1) * r;
	double py = y1 + (y2 - y1) * r;
	return (x - px) * (x - px) + (py - y) * (py - y);
}
int main(){
	int i;
	double r1,r2=0,pi=3.1415926535;
	read(n);scanf("%lf%lf",&st.x,&st.y);
	for(i=0;i<n;i++){
		scanf("%lf%lf",&p[i].x,&p[i].y);
		r2=max(r2,bian(p[i],st));
	}
	r1=r2;
	for(i=0;i<n-1;i++)r1=min(r1,PointToSegDist(st.x,st.y,p[i].x,p[i].y,p[i+1].x,p[i+1].y));
	r1=min(r1,PointToSegDist(st.x,st.y,p[0].x,p[0].y,p[n-1].x,p[n-1].y));
	double ans=pi*(r2-r1);
	printf("%.8lf",ans);
    return 0;
}

 

發佈了37 篇原創文章 · 獲贊 11 · 訪問量 4784
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章