BZOJ4107 : [Wf2015]Asteroids
首先将速度相减,变成A在动而B不动,若速度为0则显然永远不会相交。
枚举A的每个点以及B的每条线段,计算这三个点共线的时刻。
将时刻排序,对于每个区间进行三分,用半平面交计算相交面积。
注意特判相交面积为0但是存在交点的情况。
时间复杂度$O(n^4\log^2n)$。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=200;
const double eps=1e-9;
int sgn(double x){
if(x<-eps)return -1;
if(x>eps)return 1;
return 0;
}
int n,m,cnt,i,j,vx,vy,x,y;double q[N],ans=-1,anst;
struct vec{
double x,y;
vec(){x=y=0;}
vec(double _x,double _y){x=_x,y=_y;}
vec operator+(vec v){return vec(x+v.x,y+v.y);}
vec operator-(vec v){return vec(x-v.x,y-v.y);}
vec operator*(double v){return vec(x*v,y*v);}
vec operator/(double v){return vec(x/v,y/v);}
double operator*(vec v){return x*v.x+y*v.y;}
double len(){return hypot(x,y);}
double len_sqr(){return x*x+y*y;}
}a[N],b[N],c[N],v,o;
double cross(vec a,vec b){return a.x*b.y-a.y*b.x;}
bool point_on_segment(vec p,vec a,vec b){
return sgn(cross(b-a,p-a))==0&&sgn((p-a)*(p-b))<=0;
}
int has_intersection(vec a,vec b,vec p,vec q){
int d1=sgn(cross(b-a,p-a)),d2=sgn(cross(b-a,q-a)),
d3=sgn(cross(q-p,a-p)),d4=sgn(cross(q-p,b-p));
if(d1*d2<0&&d3*d4<0)return 1;
if(d1==0&&point_on_segment(p,a,b))return -1;
if(d2==0&&point_on_segment(q,a,b))return -1;
if(d3==0&&point_on_segment(a,p,q))return -1;
if(d4==0&&point_on_segment(b,p,q))return -1;
return 0;
}
int line_intersection(vec a,vec b,vec p,vec q,vec&o){
double U=cross(p-a,q-p),D=cross(b-a,q-p);
if(sgn(D)==0)return 0;
o=a+(b-a)*(U/D);
return 1;
}
struct P{
double x,y;
P(){x=y=0;}
P(double _x,double _y){x=_x,y=_y;}
P(vec p){x=p.x,y=p.y;}
P operator-(const P&a)const{return P(x-a.x,y-a.y);}
P operator+(const P&a)const{return P(x+a.x,y+a.y);}
P operator*(double a)const{return P(x*a,y*a);}
};
namespace Halfplane{
P p[N],a[N];
struct L{
P p,v;double a;
L(){}
L(P _p,P _v){p=_p,v=_v;}
bool operator<(const L&b)const{return a<b.a;}
void cal(){a=atan2(v.y,v.x);}
}line[N],q[N];
int cl;
double cross(const P&a,const P&b){return a.x*b.y-a.y*b.x;}
void newL(const P&a,const P&b){line[++cl]=L(a,b-a);}
bool left(const P&p,const L&l){return cross(l.v,p-l.p)>0;}
P pos(const L&a,const L&b){
P x=a.p-b.p;
double t=cross(b.v,x)/cross(a.v,b.v);
return a.p+a.v*t;
}
double halfplane(){
for(int i=1;i<=cl;i++)line[i].cal();
sort(line+1,line+cl+1);
int h=1,t=1;
q[1]=line[1];
for(int i=2;i<=cl;i++){
while(h<t&&!left(p[t-1],line[i]))t--;
while(h<t&&!left(p[h],line[i]))h++;
if(fabs(cross(q[t].v,line[i].v))<eps)q[t]=left(q[t].p,line[i])?q[t]:line[i];
else q[++t]=line[i];
if(h<t)p[t-1]=pos(q[t],q[t-1]);
}
while(h<t&&!left(p[t-1],q[h]))t--;
p[t]=pos(q[t],q[h]);
if(t-h<=1)return -1;
double ans=0;
for(int i=h;i<t;i++)ans+=cross(p[i],p[i+1]);
return ans+cross(p[t],p[h]);
}
}
double cal(double T){
if(!sgn(T))return -1;
double ret=-1;
int i,j;
for(i=0;i<=n;i++)c[i]=a[i]+(v*T);
for(i=0;i<n;i++)for(j=0;j<m;j++)if(has_intersection(c[i],c[i+1],b[j],b[j+1]))ret=0;
Halfplane::cl=0;
for(i=0;i<n;i++)Halfplane::newL(P(c[i+1]),P(c[i]));
for(i=0;i<m;i++)Halfplane::newL(P(b[i+1]),P(b[i]));
ret=max(ret,Halfplane::halfplane());
if(sgn(ret-ans)>0||(sgn(ret-ans)==0&&T<anst))ans=ret,anst=T;
return ret;
}
int main(){
scanf("%d",&n);
for(i=0;i<n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);a[n]=a[0];
scanf("%d%d",&vx,&vy);
scanf("%d",&m);
for(i=0;i<m;i++)scanf("%lf%lf",&b[i].x,&b[i].y);b[m]=b[0];
scanf("%d%d",&x,&y);
vx-=x,vy-=y;
if(!vx&&!vy)return puts("never"),0;
v=vec(vx,vy);
q[cnt=1]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)if(line_intersection(a[i],a[i]+v,b[j],b[j+1],o))
q[++cnt]=(o-a[i]).len()/v.len();
sort(q+1,q+cnt+1);
for(i=1;i<=cnt;i++)cal(q[i]);
for(i=1;i<cnt;i++){
double l=q[i],r=q[i+1];
while(l+1e-6<r){
double len=(r-l)/3,m1=l+len,m2=r-len;
double f1=cal(m1),f2=cal(m2);
if(sgn(f1-f2)>=0)r=m2;else l=m1;
}
}
if(ans<-0.5)puts("never");else printf("%.6f",anst);
return 0;
}
BZOJ4107 : [Wf2015]Asteroids的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POJ 3041 Asteroids
最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted: ...
- Asteroids(匈牙利算法入门)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16211 Accepted: 8819 Descri ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 3041 Asteroids(最小点覆盖集)
Asteroids Time Limit: 1000MS Mem ...
- Asteroids (最小覆盖)
题目很简单,但是需要推到出二分图最大匹配 = 最小覆盖 最小覆盖:证明过程http://blog.sina.com.cn/s/blog_51cea4040100h152.html Descriptio ...
- poj 3041 Asteroids(最小点覆盖)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- HDU-1240 Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
随机推荐
- AJAX JSON类型返回
文本样式和下拉样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- jQuery – 7.动态创建Dom、删除节点
动态创建Dom节点 1.使用$(html字符串)来创建Dom节点 2.append方法用来在元素的末尾追加元素 案例:动态生成网站列表 3.prepend,在元素的开始 ...
- bt和wifi的共存
转自:http://bbs.52rd.com/Thread-291892-1-1.html 蓝牙和802.11b/g/n都可能工作在2.4GISM,可能互相干扰.干扰的典型应用之一是VOIP,用手机的 ...
- Linux获取当前用户信息函数
转自:http://net.pku.edu.cn/~yhf/linux_c/function/07.html endgrent(关闭组文件) 相关函数 getgrent,setgrent 表头文件 # ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- 【PHP绘图技术&&验证码绘制】
PHP绘图是比较简单的事情,基本绘图如直线.圆.矩形.弧线.填充矩形.填充扇形.非中文字的打印.中文文字的打印在在下面的代码中会纤细讲解. 需要支持中文的字体,可以到windows自带的字体库中找,并 ...
- <转>Hibernate的优、缺点(局限性)
本文原文链接:http://hi.baidu.com/ko22223/item/dd9f6900015adc036d904877 一.Hibernate是JDBC的轻量级的对象封装,它是一个独立的对象 ...
- oracle pctfree和pctused详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- [Liferay6.2]Liferay入门级portlet开发示例
什么是Portlet 来自百度百科(http://baike.baidu.com/view/58961.htm)的定义如下: portlet是基于java的web组件,处理request并产生动态内容 ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...