bzoj4948: World Final2017 A
求简单多边形内的最长线段长度
显然存在一组最优解,使其所在直线经过多边形的两个端点,枚举这两个端点,求出直线和多边形的有效交点,从而得出直线有哪些部分在多边形内(含边界)。
由于多边形的一些边可能与直线重合,求交需要一些分类讨论。
#include<bits/stdc++.h>
typedef long long i64;
typedef double ld;
struct pos{
int x,y;
ld abs(){return sqrt(i64(x)*x+i64(y)*y);}
}ps[];
pos operator-(const pos&a,const pos&b){return (pos){a.x-b.x,a.y-b.y};}
i64 operator*(const pos&a,const pos&b){return i64(a.x)*b.y-i64(a.y)*b.x;}
int sgn(i64 x){return x<?-:x>;}
int n,t[],kp;
ld ans=,ks[];
void maxs(ld&x,ld y){if(x<y)x=y;}
#define F(a) ps[a]*ps[a+1]/ld(p*(ps[a+1]-ps[a]))*pl
void chk(pos p){
kp=;
ld pl=p.abs();
for(int i=;i<n+;++i)t[i]=sgn(p*ps[i]);
for(int i=,z;i<n;++i)if(t[i]&&t[i]!=t[i+]){
for(z=i+;!t[z];++z);
if(t[z]!=t[i]){
if(z-i<)ks[kp++]=F(i);
else{
ld a0=F(i),a1=F(z-);
ks[kp++]=(a0<a1)==(t[i]>)?a0:a1;
}
}else if(z-i==){
ld a0=F(i),a1=F(z-);
if((a0<a1)==(t[i]>))maxs(ans,fabs(a1-a0));
}
}
std::sort(ks,ks+kp);
for(int i=;i<kp;i+=)maxs(ans,ks[i+]-ks[i]);
}
int main(){
scanf("%d",&n);
for(int i=;i<n;++i)scanf("%d%d",&ps[i].x,&ps[i].y);
for(int i=;i<n;++i){
pos o=ps[i];
for(int j=;j<n;++j)ps[j]=ps[j]-o;
for(int j=;j<;++j)ps[n+j]=ps[j];
for(int j=i+;j<n;++j)chk(ps[j]);
}
printf("%.8f",ans);
return ;
}
bzoj4948: World Final2017 A的更多相关文章
随机推荐
- CodeForces - 441E:Valera and Number (DP&数学期望&二进制)
Valera is a coder. Recently he wrote a funny program. The pseudo code for this program is given belo ...
- github如何删除新建仓库(致新手)
github作为开发人员的必备用具.那么,作为一个新手如何删除github中建立的仓库呢? 1.以删除My test为例
- POJ 2456: Aggressive cows(二分,贪心)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20485 Accepted: 9719 ...
- C# 敏捷1
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...
- 《DSP using MATLAB》Problem 5.22
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% O ...
- LeetCode - Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- Centos7提示swap交换空间不足解决方法
一张图就能解决的问题,就不多bb了
- C# 键盘响应事件及键值对照表
键盘响应事件总结 键盘响应事件是在用户按下某个键后触发的事件,可以是任意操作,但不是任意键都可以被捕获的. 原型:public event KeyPressEventHandler KeyPress ...
- 模拟实现memcpy 与 memmove
模拟实现memcpy 与 memmove 1.str系列的函数只能处理字符串——>必须带有'\0'2.memcpy内存处理函数:不涉及'\0',需要包含头文件 string.h3.source的 ...
- 替换元素(replace,replace_if,replace_copy,replace_copy_if)
replace 审阅range中的每个元素,把old_value替换为new_value template <class ForwardIterator,class T> void rep ...