UVA 11178 Morley 定理
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld Pi = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); } //不分方向,返回正数
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A 不是 0向量
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):实部 imag(p):虚部 conj(p):共轭
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*欧拉公式: V+F-E=2
V-点数 F面数 E边数 */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r;
C(P C,double r):c(c),r(r){}
P point(double a) {
return P(c.x+cos(a)*r,c.y*sin(a)*r);
}
};
P getD(P A,P B,P C) {
V v1=C-B;
double a1=Angle(A-B,v1);
v1 = Rotate(v1,a1/3);
V v2 = B-C;
double a2 = Angle(A-C,v2);
v2 = Rotate(v2,-a2/3);
return GetLineIntersection(B,v1,C,v2);
}
int main()
{
// freopen("uva11178.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
P A=read_point(),B=read_point(),C=read_point(),D,E,F;
D=getD(A,B,C);
E=getD(B,C,A);
F=getD(C,A,B);
printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",D.x,D.y,E.x,E.y,F.x,F.y);
}
return 0;
}
LA 3263 That Nice Euler Circuit, Shanghai 2004, 欧拉定理
欧拉公式:设平面图顶点数,边数,面数分别为V,E,F,
V+F−E=2
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld Pi = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); } //不分方向,返回正数
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A 不是 0向量
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):实部 imag(p):虚部 conj(p):共轭
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*欧拉公式: V+F-E=2
V-点数 F面数 E边数 */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r;
C(P C,double r):c(c),r(r){}
P point(double a) {
return P(c.x+cos(a)*r,c.y*sin(a)*r);
}
};
int n;
P a[410];
int main()
{
// freopen("uva1342.in","r",stdin);
// freopen(".out","w",stdout);
int kcase=1;
while(cin>>n&&n) {
Rep(i,n) a[i]=read_point(); --n;
int Vn=n,En=n;
vector<P> v;
Rep(i,n) v.pb(a[i]);
Rep(i,n) Fork(j,i+1,n-1) {
if (SegmentProperIntersection(a[i],a[i+1],a[j],a[j+1]) ) {
v.pb(GetLineIntersection(a[i],a[i+1]-a[i],a[j],a[j+1]-a[j]));
}
}
sort(v.begin(),v.end());
Vn=unique(v.begin(),v.end())-v.begin();
Rep(i,Vn) Rep(j,n) if (OnSegment(v[i],a[j],a[j+1])) ++En;
printf("Case %d: There are %d pieces.\n",kcase++,2+En-Vn);
}
return 0;
}
UVA 11796 Dog Distance
注意固定一只dog静止,让另一只走
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case %d: %.0lf\n",kcase,ans);
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld Pi = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); } //不分方向,返回正数
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A 不是 0向量
// normal-单位法线向量
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):实部 imag(p):虚部 conj(p):共轭
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*欧拉公式: V+F-E=2
V-点数 F面数 E边数 */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r;
C(P C,double r):c(c),r(r){}
P point(double a) {
return P(c.x+cos(a)*r,c.y*sin(a)*r);
}
};
const int MAXN = 50+10;
int n,m;
P p[MAXN],q[MAXN];
double Min,Max;
void Update(P p,P a,P b) {
Min=min(Min,DistanceToSegment(p,a,b));
Max=max(max(Length(p-a),Length(p-b)),(ld)Max);
}
int main()
{
// freopen("uva11796.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
n=read(),m=read();
Rep(i,n) p[i]=read_point();
Rep(i,m) q[i]=read_point();
double la=0,lb=0;
Rep(i,n-1) la+=Length(p[i+1]-p[i]);
Rep(i,m-1) lb+=Length(q[i+1]-q[i]);
int Sa=0,Sb=0;
P Pa=p[0],Pb=q[0];
Min=1e9,Max=0;
while(Sa<n-1&&Sb<m-1) {
double La=Length(p[Sa+1]-Pa),Lb=Length(q[Sb+1]-Pb);
double Time=min(La/la,Lb/lb);
V Va=(p[Sa+1]-Pa)/La*Time*la;
V Vb=(q[Sb+1]-Pb)/Lb*Time*lb;
Update(Pa,Pb,Pb+Vb-Va);
Pa=Pa+Va;
Pb=Pb+Vb;
if (Pa==p[Sa+1]) Sa++;
if (Pb==q[Sb+1]) Sb++;
}
Pr(kcase,Max-Min);
}
return 0;
}
UVA 12304 2D Geometry 110 in 1! , 内接圆和外接圆
注意我修改了getLineCircleIntersection()中的判定
还有这题输出好麻烦
这题没模板能写晕掉
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A 不是 0向量
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):实部 imag(p):虚部 conj(p):共轭
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*欧拉公式: V+F-E=2
V-点数 F面数 E边数 */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r,x,y;
C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
P point(double a) {
return P(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
struct Line{
P p;
V v;
double ang;
Line(){}
Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
bool operator<(const Line & L) const {
return ang<L.ang;
}
P point(double a) {
return p+v*a;
}
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector<P> & sol) {
if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
sol.pb(GetLineProjection(cir.c,L.p,L.p+L.v));
return 1;
}
double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
double delta = f*f - 4*e*g;
if (dcmp(delta)<0) return 0;
else if (dcmp(delta)==0) {
t1 = t2 = -f / (2*e); sol.pb(L.point(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2));
return 2;
}
double angle(V v) {return atan2(v.y,v.x);}
int getCircleCircleIntersection(C C1,C C2,vector<P>& sol) {
double d = Length(C1.c-C2.c);
if (dcmp(d)==0) {
if (dcmp(C1.r - C2.r)==0) return -1; //2圆重合
return 0;
}
if (dcmp(C1.r+C2.r-d)<0) return 0;
if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;
double a = angle(C2.c-C1.c);
double da = acos((C1.r*C1.r+d*d - C2.r*C2.r)/ (2*C1.r*d));
P p1 = C1.point(a-da), p2 = C1.point(a+da);
sol.pb(p1);
if (p1==p2) return 1;
sol.pb(p2);
return 2;
}
// Tangents-切线
int getTangents(P p,C c,V* v) {
V u= c.c-p;
double dist = Length(u);
if (dist<c.r) return 0;
else if (dcmp(dist-c.r)==0) {
v[0]=Rotate(u,PI/2);
return 1;
} else {
double ang = asin(c.r / dist);
v[0]=Rotate(u,-ang);
v[1]=Rotate(u,ang);
return 2;
}
}
//这个函数假设整数坐标和整数半径
//double时要把int改成double
int getTangents(C A,C B,P* a,P* b) {
int cnt=0;
if (A.r<B.r) {swap(A,B),swap(a,b);}
int d2 = (A.c.x-B.c.x)*(A.c.x-B.c.x) + (A.c.y-B.c.y)*(A.c.y-B.c.y);
int rdiff = A.r-B.r;
int rsum = A.r+B.r;
if (d2<rdiff*rdiff) return 0;
double base = atan2(B.y-A.y,B.x-A.x);
if (d2==0 && A.r == B.r) return -1;
if (d2 == rdiff*rdiff) {
a[cnt] = A.point(base); b[cnt] = B.point(base); ++cnt;
return 1;
}
double ang = acos((A.r-B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang); ++cnt;
if (d2==rsum*rsum) {
a[cnt] = A.point(base); b[cnt] = B.point(PI+base); ++cnt;
}
else if (d2>rsum*rsum) {
double ang = acos((A.r+B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(PI+base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(PI+base-ang); ++cnt;
}
return cnt;
}
//Circumscribed-外接
C CircumscribedCircle(P p1,P p2,P p3) {
double Bx = p2.x-p1.x, By= p2.y-p1.y;
double Cx = p3.x-p1.x, Cy= p3.y-p1.y;
double D = 2*(Bx*Cy-By*Cx);
double cx = (Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D + p1.x;
double cy = (Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D + p1.y;
P p =P(cx,cy);
return C(p,Length(p1-p));
}
//Inscribed-内接
C InscribedCircle(P p1,P p2,P p3) {
double a = Length(p2-p3);
double b = Length(p3-p1);
double c = Length(p1-p2);
P p = (p1*a+p2*b+p3*c)/(a+b+c);
return C(p,DistanceToLine(p,p1,p2));
}
double torad(double deg) {
return deg/180*acos(-1);
}
//把 角度+-pi 转换到 [0,pi) 上
double radToPositive(double rad) {
if (dcmp(rad)<0) rad=ceil(-rad/PI)*PI+rad;
if (dcmp(rad-PI)>=0) rad-=floor(rad/PI)*PI;
return rad;
}
double todeg(double rad) {
return rad*180/acos(-1);
}
//(R,lat,lng)->(x,y,z)
void get_coord(double R,double lat,double lng,double &x,double &y,double &z) {
lat=torad(lat);
lng=torad(lng);
x=R*cos(lat)*cos(lng);
y=R*cos(lat)*sin(lng);
z=R*sin(lat);
}
void print(double a) {
printf("%.6lf",a);
}
void print(P p) {
printf("(%.6lf,%.6lf)",p.x,p.y);
}
template<class T>
void print(vector<T> v) {
sort(v.begin(),v.end());
putchar('[');
int n=v.size();
Rep(i,n) {
print(v[i]);
if (i<n-1) putchar(',');
}
puts("]");
}
Line LineTranslation(Line l,V v) {
l.p=l.p+v;
return l;
}
void CircleThroughAPointAndTangentToALineWithRadius(P p,Line l,double r) {
V e=Normal(l.v);
Line l1=LineTranslation(l,e*r),l2=LineTranslation(l,e*(-r));
vector<P> sol;
double t1,t2;
getLineCircleIntersection(l1,C(p,r),t1,t2,sol);
getLineCircleIntersection(l2,C(p,r),t1,t2,sol);
print(sol);
}
void CircleTangentToTwoLinesWithRadius(Line l1,Line l2,double r) {
V e1=Normal(l1.v),e2=Normal(l2.v);
Line L1[2]={LineTranslation(l1,e1*r),LineTranslation(l1,e1*(-r))},
L2[2]={LineTranslation(l2,e2*r),LineTranslation(l2,e2*(-r))};
vector<P> sol;
Rep(i,2) Rep(j,2) sol.pb(GetLineIntersection(L1[i].p,L1[i].v,L2[j].p,L2[j].v));
print(sol);
}
void CircleTangentToTwoDisjointCirclesWithRadius(C c1,C c2,double r) {
c1.r+=r; c2.r+=r;
vector<P> sol;
getCircleCircleIntersection(c1,c2,sol);
print(sol);
}
int main()
{
// freopen("uva12304.in","r",stdin);
// freopen(".out","w",stdout);
string s;
while(cin>>s) {
if (s=="CircumscribedCircle") {
P p1=read_point(),p2=read_point(),p3=read_point();
C c=CircumscribedCircle(p1,p2,p3);
printf("(%.6lf,%.6lf,%.6lf)\n",c.x,c.y,c.r);
}
if (s=="InscribedCircle") {
P p1=read_point(),p2=read_point(),p3=read_point();
C c=InscribedCircle(p1,p2,p3);
printf("(%.6lf,%.6lf,%.6lf)\n",c.x,c.y,c.r);
}
if (s=="TangentLineThroughPoint") {
V v[12];
double xc,yc,r,xp,yp;
scanf("%lf%lf%lf%lf%lf",&xc,&yc,&r,&xp,&yp);
int n=getTangents(P(xp,yp),C(P(xc,yc),r),v);
vector<double> vec;
Rep(i,n) vec.pb(todeg(radToPositive(angle(v[i]))));
print(vec);
}
if (s=="CircleThroughAPointAndTangentToALineWithRadius") {
double xp,yp,x1,y1,x2,y2,r;
scanf("%lf%lf%lf%lf%lf%lf%lf",&xp,&yp,&x1,&y1,&x2,&y2,&r);
CircleThroughAPointAndTangentToALineWithRadius(P(xp,yp),Line(P(x1,y1),V(x2-x1,y2-y1)),r);
}
if (s=="CircleTangentToTwoLinesWithRadius") {
P p1=read_point(),v1=read_point()-p1,p2=read_point(),v2=read_point()-p2;
double r;
scanf("%lf",&r);
CircleTangentToTwoLinesWithRadius(Line(p1,v1),Line(p2,v2),r);
}
if (s=="CircleTangentToTwoDisjointCirclesWithRadius") {
double x1,y1,r1,x2,y2,r2,r;
scanf("%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2,&r);
CircleTangentToTwoDisjointCirclesWithRadius(C(P(x1,y1),r1),C(P(x2,y2),r2),r);
}
}
return 0;
}
UVA 1308 Viva Confetti
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
ld x,y;
P(long double x=0,long double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const ld eps=1e-14;
int dcmp(ld x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A 不是 0向量
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):实部 imag(p):虚部 conj(p):共轭
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//规范相交-线段相交且交点不在端点
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*欧拉公式: V+F-E=2
V-点数 F面数 E边数 */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r,x,y;
C(){}
C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
P point(double a) {
return P(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
struct Line{
P p;
V v;
double ang;
Line(){}
Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
bool operator<(const Line & L) const {
return ang<L.ang;
}
P point(double a) {
return p+v*a;
}
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector<P> & sol) {
if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
sol.pb(GetLineProjection(cir.c,L.p,L.p+L.v));
return 1;
}
double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
double delta = f*f - 4*e*g;
if (dcmp(delta)<0) return 0;
else if (dcmp(delta)==0) {
t1 = t2 = -f / (2*e); sol.pb(L.point(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2));
return 2;
}
double angle(V v) {return atan2(v.y,v.x);}
int getCircleCircleIntersection(C C1,C C2,vector<P>& sol) {
double d = Length(C1.c-C2.c);
if (dcmp(d)==0) {
if (dcmp(C1.r - C2.r)==0) return -1; //2圆重合
return 0;
}
if (dcmp(C1.r+C2.r-d)<0) return 0;
if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;
double a = angle(C2.c-C1.c);
double da = acos((C1.r*C1.r+d*d - C2.r*C2.r)/ (2*C1.r*d));
P p1 = C1.point(a-da), p2 = C1.point(a+da);
sol.pb(p1);
if (p1==p2) return 1;
sol.pb(p2);
return 2;
}
// Tangents-切线
int getTangents(P p,C c,V* v) {
V u= c.c-p;
double dist = Length(u);
if (dist<c.r) return 0;
else if (dcmp(dist-c.r)==0) {
v[0]=Rotate(u,PI/2);
return 1;
} else {
double ang = asin(c.r / dist);
v[0]=Rotate(u,-ang);
v[1]=Rotate(u,ang);
return 2;
}
}
//这个函数假设整数坐标和整数半径
//double时要把int改成double
int getTangents(C A,C B,P* a,P* b) {
int cnt=0;
if (A.r<B.r) {swap(A,B),swap(a,b);}
int d2 = (A.c.x-B.c.x)*(A.c.x-B.c.x) + (A.c.y-B.c.y)*(A.c.y-B.c.y);
int rdiff = A.r-B.r;
int rsum = A.r+B.r;
if (d2<rdiff*rdiff) return 0;
double base = atan2(B.y-A.y,B.x-A.x);
if (d2==0 && A.r == B.r) return -1;
if (d2 == rdiff*rdiff) {
a[cnt] = A.point(base); b[cnt] = B.point(base); ++cnt;
return 1;
}
double ang = acos((A.r-B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang); ++cnt;
if (d2==rsum*rsum) {
a[cnt] = A.point(base); b[cnt] = B.point(PI+base); ++cnt;
}
else if (d2>rsum*rsum) {
double ang = acos((A.r+B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(PI+base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(PI+base-ang); ++cnt;
}
return cnt;
}
//Circumscribed-外接
C CircumscribedCircle(P p1,P p2,P p3) {
double Bx = p2.x-p1.x, By= p2.y-p1.y;
double Cx = p3.x-p1.x, Cy= p3.y-p1.y;
double D = 2*(Bx*Cy-By*Cx);
double cx = (Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D + p1.x;
double cy = (Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D + p1.y;
P p =P(cx,cy);
return C(p,Length(p1-p));
}
//Inscribed-内接
C InscribedCircle(P p1,P p2,P p3) {
double a = Length(p2-p3);
double b = Length(p3-p1);
double c = Length(p1-p2);
P p = (p1*a+p2*b+p3*c)/(a+b+c);
return C(p,DistanceToLine(p,p1,p2));
}
double torad(double deg) {
return deg/180*acos(-1);
}
//把 角度+-pi 转换到 [0,pi) 上
double radToPositive(double rad) {
if (dcmp(rad)<0) rad=ceil(-rad/PI)*PI+rad;
if (dcmp(rad-PI)>=0) rad-=floor(rad/PI)*PI;
return rad;
}
double todeg(double rad) {
return rad*180/acos(-1);
}
//(R,lat,lng)->(x,y,z)
void get_coord(double R,double lat,double lng,double &x,double &y,double &z) {
lat=torad(lat);
lng=torad(lng);
x=R*cos(lat)*cos(lng);
y=R*cos(lat)*sin(lng);
z=R*sin(lat);
}
void print(double a) {
printf("%.6lf",a);
}
void print(P p) {
printf("(%.6lf,%.6lf)",p.x,p.y);
}
template<class T>
void print(vector<T> v) {
sort(v.begin(),v.end());
putchar('[');
int n=v.size();
Rep(i,n) {
print(v[i]);
if (i<n-1) putchar(',');
}
puts("]");
}
// 把直线沿v平移
// Translation-平移
Line LineTranslation(Line l,V v) {
l.p=l.p+v;
return l;
}
void CircleThroughAPointAndTangentToALineWithRadius(P p,Line l,double r,vector<P>& sol) {
V e=Normal(l.v);
Line l1=LineTranslation(l,e*r),l2=LineTranslation(l,e*(-r));
double t1,t2;
getLineCircleIntersection(l1,C(p,r),t1,t2,sol);
getLineCircleIntersection(l2,C(p,r),t1,t2,sol);
}
void CircleTangentToTwoLinesWithRadius(Line l1,Line l2,double r,vector<P>& sol) {
V e1=Normal(l1.v),e2=Normal(l2.v);
Line L1[2]={LineTranslation(l1,e1*r),LineTranslation(l1,e1*(-r))},
L2[2]={LineTranslation(l2,e2*r),LineTranslation(l2,e2*(-r))};
Rep(i,2) Rep(j,2) sol.pb(GetLineIntersection(L1[i].p,L1[i].v,L2[j].p,L2[j].v));
}
void CircleTangentToTwoDisjointCirclesWithRadius(C c1,C c2,double r,vector<P>& sol) {
c1.r+=r; c2.r+=r;
getCircleCircleIntersection(c1,c2,sol);
}
bool isPointInOrOnCircle(P p,C c) {
return dcmp(Length(p-c.c)-c.r)<=0;
}
int n;
C c[200];
bool b[200];
bool check_point(P p) {
RepD(i,n-1) {
if (isPointInOrOnCircle(p,c[i])) return b[i]=1;
}
}
int main()
{
// freopen("uva1308.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n&&n) {
Rep(i,n) {
double x,y,r;
scanf("%lf%lf%lf",&x,&y,&r);
c[i]=C(P(x,y),r);
}
vector<P> sol;
MEM(b)
Rep(i,n) {
vector<P> sol;
Fork(j,i+1,n-1) getCircleCircleIntersection(c[i],c[j],sol);
sort(sol.begin(),sol.end());
int sz=unique(sol.begin(),sol.end())-sol.begin();
vector<ld> rad;
Rep(j,sz) rad.pb(angle(sol[j]-c[i].c));
rad.pb(0); ++sz;
sort(rad.begin(),rad.end());
Rep(j,sz) {
int k=(j+1)%sz;
ld m=(rad[j]+rad[k])/2;
if (rad[j]>rad[k]) m+=PI;
P p=c[i].point(m);
bool flag=0;
Fork(l,i+1,n-1) {
ld re=Dot(p-c[l].c,p-c[l].c)-(c[l].r)*(c[l].r);
if (isPointInOrOnCircle(p,c[l])) {flag=1; break;}
}
if (!flag) {
check_point(p+(p-c[i].c)*eps);
check_point(p-(p-c[i].c)*eps);
}
}
}
int ans=0; Rep(i,n) ans+=b[i];
cout<<ans<<endl;
}
return 0;
}
LA 2402 Fishnet
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<complex>
#include<functional>
#include<algorithm>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
P GetLineIntersectionB(P p,V v,P Q,V w){
return GetLineIntersection(p,v-p,Q,w-Q);
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double PolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r,x,y;
C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
P point(double a) {
return P(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
struct Line{
P p;
V v;
double ang;
Line(){}
Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
bool operator<(const Line & L) const {
return ang<L.ang;
}
P point(double a) {
return p+v*a;
}
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector<P> & sol) {
if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
sol.pb(GetLineProjection(cir.c,L.p,L.p+L.v));
return 1;
}
double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
double delta = f*f - 4*e*g;
if (dcmp(delta)<0) return 0;
else if (dcmp(delta)==0) {
t1 = t2 = -f / (2*e); sol.pb(L.point(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2));
return 2;
}
double angle(V v) {return atan2(v.y,v.x);}
typedef vector<P> Polygon ;
#define MAXN (300+10)
int n;
double a[MAXN],b[MAXN],c[MAXN],d[MAXN];
int main()
{
// freopen("uva1301.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n&&n) {
For(i,n) cin>>a[i];
For(i,n) cin>>b[i];
For(i,n) cin>>c[i];
For(i,n) cin>>d[i];
a[0]=b[0]=c[0]=d[0]=0;
a[n+1]=b[n+1]=c[n+1]=d[n+1]=1;
double an=0;
Rep(i,n+1) {
Rep(j,n+1) {
P p1=GetLineIntersectionB(P(a[i],0 ) , P(b[i],1), P(0,c[j]), P(1,d[j]) );
P p2=GetLineIntersectionB(P(a[i+1],0 ) , P(b[i+1],1), P(0,c[j]), P(1,d[j]) );
P p3=GetLineIntersectionB(P(a[i],0 ) , P(b[i],1), P(0,c[j+1]), P(1,d[j+1]) );
P p4=GetLineIntersectionB(P(a[i+1],0 ) , P(b[i+1],1), P(0,c[j+1]), P(1,d[j+1]) );
double ans=(Area2(p1,p2,p3))-(Area2(p2,p3,p4));
an=max(an,ans);
}
}
printf("%.6f\n",an/2);
}
return 0;
}
UVA 10969 Sweet Dream
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
ld x,y;
P(long double x=0,long double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const ld eps=1e-14;
int dcmp(ld x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A ²»ÊÇ 0ÏòÁ¿
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):ʵ²¿ imag(p):Ð鲿 conj(p):¹²éî
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(ƽÐÐ)ʱ,²»Äܵ÷Õâ¸öº¯Êý
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//¹æ·¶Ïཻ-Ï߶ÎÏཻÇÒ½»µã²»Ôڶ˵ã
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//µãÔÚÏ߶ÎÉÏ£¨²»°üº¬¶Ëµã£©
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*Å·À¹«Ê½£º V+F-E=2
V-µãÊý FÃæÊý E±ßÊý */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r,x,y;
C(){}
C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
P point(double a) {
return P(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
struct Line{
P p;
V v;
double ang;
Line(){}
Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
bool operator<(const Line & L) const {
return ang<L.ang;
}
P point(double a) {
return p+v*a;
}
};
int getLineCircleIntersection(Line L,C cir,double &t1,double &t2,vector<P> & sol) {
if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
sol.pb(GetLineProjection(cir.c,L.p,L.p+L.v));
return 1;
}
double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
double delta = f*f - 4*e*g;
if (dcmp(delta)<0) return 0;
else if (dcmp(delta)==0) {
t1 = t2 = -f / (2*e); sol.pb(L.point(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e); sol.pb(L.point(t2));
return 2;
}
double angle(V v) {return atan2(v.y,v.x);}
int getCircleCircleIntersection(C C1,C C2,vector<P>& sol) {
double d = Length(C1.c-C2.c);
if (dcmp(d)==0) {
if (dcmp(C1.r - C2.r)==0) return -1; //2Ô²ÖغÏ
return 0;
}
if (dcmp(C1.r+C2.r-d)<0) return 0;
if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;
double a = angle(C2.c-C1.c);
double da = acos((C1.r*C1.r+d*d - C2.r*C2.r)/ (2*C1.r*d));
P p1 = C1.point(a-da), p2 = C1.point(a+da);
sol.pb(p1);
if (p1==p2) return 1;
sol.pb(p2);
return 2;
}
// Tangents-ÇÐÏß
int getTangents(P p,C c,V* v) {
V u= c.c-p;
double dist = Length(u);
if (dist<c.r) return 0;
else if (dcmp(dist-c.r)==0) {
v[0]=Rotate(u,PI/2);
return 1;
} else {
double ang = asin(c.r / dist);
v[0]=Rotate(u,-ang);
v[1]=Rotate(u,ang);
return 2;
}
}
//Õâ¸öº¯Êý¼ÙÉèÕûÊý×ø±êºÍÕûÊý°ë¾¶
//doubleʱҪ°Ñint¸Ä³Édouble
int getTangents(C A,C B,P* a,P* b) {
int cnt=0;
if (A.r<B.r) {swap(A,B),swap(a,b);}
int d2 = (A.c.x-B.c.x)*(A.c.x-B.c.x) + (A.c.y-B.c.y)*(A.c.y-B.c.y);
int rdiff = A.r-B.r;
int rsum = A.r+B.r;
if (d2<rdiff*rdiff) return 0;
double base = atan2(B.y-A.y,B.x-A.x);
if (d2==0 && A.r == B.r) return -1;
if (d2 == rdiff*rdiff) {
a[cnt] = A.point(base); b[cnt] = B.point(base); ++cnt;
return 1;
}
double ang = acos((A.r-B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(base-ang); ++cnt;
if (d2==rsum*rsum) {
a[cnt] = A.point(base); b[cnt] = B.point(PI+base); ++cnt;
}
else if (d2>rsum*rsum) {
double ang = acos((A.r+B.r)/sqrt(d2));
a[cnt] = A.point(base+ang); b[cnt] = B.point(PI+base+ang); ++cnt;
a[cnt] = A.point(base-ang); b[cnt] = B.point(PI+base-ang); ++cnt;
}
return cnt;
}
//Circumscribed-Íâ½Ó
C CircumscribedCircle(P p1,P p2,P p3) {
double Bx = p2.x-p1.x, By= p2.y-p1.y;
double Cx = p3.x-p1.x, Cy= p3.y-p1.y;
double D = 2*(Bx*Cy-By*Cx);
double cx = (Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D + p1.x;
double cy = (Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D + p1.y;
P p =P(cx,cy);
return C(p,Length(p1-p));
}
//Inscribed-ÄÚ½Ó
C InscribedCircle(P p1,P p2,P p3) {
double a = Length(p2-p3);
double b = Length(p3-p1);
double c = Length(p1-p2);
P p = (p1*a+p2*b+p3*c)/(a+b+c);
return C(p,DistanceToLine(p,p1,p2));
}
double torad(double deg) {
return deg/180*acos(-1);
}
//°Ñ ½Ç¶È+-pi ת»»µ½ [0,pi) ÉÏ
double radToPositive(double rad) {
if (dcmp(rad)<0) rad=ceil(-rad/PI)*PI+rad;
if (dcmp(rad-PI)>=0) rad-=floor(rad/PI)*PI;
return rad;
}
double todeg(double rad) {
return rad*180/acos(-1);
}
//(R,lat,lng)->(x,y,z)
void get_coord(double R,double lat,double lng,double &x,double &y,double &z) {
lat=torad(lat);
lng=torad(lng);
x=R*cos(lat)*cos(lng);
y=R*cos(lat)*sin(lng);
z=R*sin(lat);
}
void print(double a) {
printf("%.6lf",a);
}
void print(P p) {
printf("(%.6lf,%.6lf)",p.x,p.y);
}
template<class T>
void print(vector<T> v) {
sort(v.begin(),v.end());
putchar('[');
int n=v.size();
Rep(i,n) {
print(v[i]);
if (i<n-1) putchar(',');
}
puts("]");
}
// °ÑÖ±ÏßÑØvƽÒÆ
// Translation-ƽÒÆ
Line LineTranslation(Line l,V v) {
l.p=l.p+v;
return l;
}
void CircleThroughAPointAndTangentToALineWithRadius(P p,Line l,double r,vector<P>& sol) {
V e=Normal(l.v);
Line l1=LineTranslation(l,e*r),l2=LineTranslation(l,e*(-r));
double t1,t2;
getLineCircleIntersection(l1,C(p,r),t1,t2,sol);
getLineCircleIntersection(l2,C(p,r),t1,t2,sol);
}
void CircleTangentToTwoLinesWithRadius(Line l1,Line l2,double r,vector<P>& sol) {
V e1=Normal(l1.v),e2=Normal(l2.v);
Line L1[2]={LineTranslation(l1,e1*r),LineTranslation(l1,e1*(-r))},
L2[2]={LineTranslation(l2,e2*r),LineTranslation(l2,e2*(-r))};
Rep(i,2) Rep(j,2) sol.pb(GetLineIntersection(L1[i].p,L1[i].v,L2[j].p,L2[j].v));
}
void CircleTangentToTwoDisjointCirclesWithRadius(C c1,C c2,double r,vector<P>& sol) {
c1.r+=r; c2.r+=r;
getCircleCircleIntersection(c1,c2,sol);
}
bool isPointInOrOnCircle(P p,C c) {
return dcmp(Length(p-c.c)-c.r)<=0;
}
int n;
C c[200];
int main()
{
// freopen("uva10969.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(cin>>n) {
Rep(i,n) {
double x,y,r;
scanf("%lf%lf%lf",&r,&x,&y);
c[i]=C(P(x,y),r);
}
double ans=0;
Rep(i,n) {
vector<P> sol;
Fork(j,i+1,n-1) getCircleCircleIntersection(c[i],c[j],sol);
sort(sol.begin(),sol.end());
int sz=unique(sol.begin(),sol.end())-sol.begin();
vector<ld> rad;
Rep(j,sz) rad.pb(angle(sol[j]-c[i].c));
rad.pb(0); ++sz;
rad.pb(-PI); ++sz;
rad.pb(PI); ++sz;
sort(rad.begin(),rad.end());
Rep(j,sz-1) {
int k=(j+1)%sz;
ld m=(rad[j]+rad[k])/2;
P p=c[i].point(m);
bool flag=0;
Fork(l,i+1,n-1) {
ld re=Dot(p-c[l].c,p-c[l].c)-(c[l].r)*(c[l].r);
if (isPointInOrOnCircle(p,c[l])) {flag=1;}
}
if (!flag) ans+=(rad[k]-rad[j])*c[i].r;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(3);
cout<<ans<<endl;
}
return 0;
}
UVA 11177 Fighting Against a Polygonal Monster
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
ld x,y;
P(long double x=0,long double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const ld eps=1e-14;
int dcmp(ld x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
double Area2(P A,P B,P C) {return Cross(B-A,C-A);}
V Rotate(V A,double rad) {
return V(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
// A ²»ÊÇ 0ÏòÁ¿
V Normal(V A) {
double L = Length(A);
return V(-A.y/L , A.x/L);
}
namespace complex_G{
typedef complex<double> Point;
//real(p):ʵ²¿ imag(p):Ð鲿 conj(p):¹²éî
typedef Point Vector;
double Dot(Vector A,Vector B) {return real(conj(A)*B); }
double Cross(Vector A,Vector B) {return imag(conj(A)*B); }
Vector Rotate(Vector A,double rad) {return A*exp(Point(0,rad)); }
}
//Cross(v,w)==0(ƽÐÐ)ʱ,²»Äܵ÷Õâ¸öº¯Êý
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double DistanceToLine(P p,P A,P B) {
V v1 = B-A, v2 = p-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabs(Cross(v1,v2) ) / Length(v1);
}
P GetLineProjection(P p,P A,P B) {
V v=B-A;
return A+v*(Dot(v,p-A)/Dot(v,v));
}
//¹æ·¶Ïཻ-Ï߶ÎÏཻÇÒ½»µã²»Ôڶ˵ã
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//µãÔÚÏ߶ÎÉÏ£¨²»°üº¬¶Ëµã£©
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(P *p,int n) {
double area=0;
For(i,n-2) area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
/*Å·À¹«Ê½£º V+F-E=2
V-µãÊý FÃæÊý E±ßÊý */
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
struct C{
P c;
double r,x,y;
C(){}
C(P c,double r):c(c),r(r),x(c.x),y(c.y){}
P point(double a) {
return P(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
struct Line{
P p;
V v;
double ang;
Line(){}
Line(P p,V v):p(p),v(v) {ang=atan2(v.y,v.x); }
bool operator<(const Line & L) const {
return ang<L.ang;
}
P point(double a) {
return p+v*a;
}
};
int getSegCircleIntersection(Line L,C cir,vector<P> & sol) {
if (dcmp(DistanceToLine(cir.c,L.p,L.p+L.v)-cir.r)==0) {
P A= GetLineProjection(cir.c,L.p,L.p+L.v);
if (OnSegment(A,L.p,L.p+L.v) || L.p==A || L.p+L.v==A )
sol.pb(A);
return sol.size();
}
double t1,t2;
double a = L.v.x, b = L.p.x - cir.c.x, c = L.v.y, d= L.p.y - cir.c.y;
double e = a*a+c*c, f = 2*(a*b + c*d), g = b*b+d*d-cir.r*cir.r;
double delta = f*f - 4*e*g;
if (dcmp(delta)<0) return 0;
else if (dcmp(delta)==0) {
t1 = -f / (2*e);
if (0<=t1&&t1<=1) {
sol.pb(L.point(t1));
}
return sol.size();
}
t1 = (-f - sqrt(delta)) / (2*e); if (0<=t1&&t1<=1) sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / (2*e); if (0<=t2&&t2<=1) sol.pb(L.point(t2));
if (0<=t2&&t2<t1&&t1<=1) swap(sol[0],sol[1]);
return sol.size();
}
double angle(V v) {return atan2(v.y,v.x);}
int isPointInOrOnCircle(P p,C c) {
return dcmp(Length(p-c.c)-c.r)<=0;
}
int n;
P p[200],O(0,0);
int main()
{
// freopen("uva11177.in","r",stdin);
// freopen(".out","w",stdout);
int kcase=1;
while(cin>>n&&n) {
double R; cin>>R;
double l=0,r=0;
Rep(i,n) {
cin>>p[i].x>>p[i].y;
r=max(r,(double)Length(p[i]));
}
while(r-l>1e-6) {
double m=(l+r)/2,ans=0;
C c=C(P(),m);
Rep(i,n) {
int j=(i+1)%n;
bool b = isPointInOrOnCircle(p[i],c);
bool b2 = isPointInOrOnCircle(p[j],c);
if (b&&b2) {
ans+=fabs(Area2(p[i],p[j],O))/2;
} else if (!b&&!b2){
Line l=Line(p[i],p[j]-p[i]);
vector<P> sol;
getSegCircleIntersection(l,c,sol);
if (SI(sol)==2) {
ans+=fabs(Area2(sol[0],sol[1],O))/2;
ans+=m*m/2*(Angle(p[i],sol[0])+Angle(sol[1],p[j]));
} else {
ans+=m*m/2*(Angle(p[i],p[j]));
}
} else {
Line l=Line(p[i],p[j]-p[i]);
vector<P> sol;
getSegCircleIntersection(l,c,sol);
if(b) {
ans+=fabs(Area2(sol[0],p[i],O))/2;
ans+=m*m/2*(Angle(sol[0],p[j]));
} else {
ans+=fabs(Area2(sol[0],p[j],O))/2;
ans+=m*m/2*(Angle(sol[0],p[i]));
}
}
}
if (ans>=R) r=m;else l=m;
}
printf("Case %d: %.2lf\n",kcase++,l);
}
return 0;
}
UVA 1359 Hills
题解有毒。。
复杂度不是O(n^3logn)?。。
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
ld PI = 3.141592653589793238462643383;
class P{
public:
double x,y;
P(double x=0,double y=0):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt(Dot(A,A)); }
friend long double Angle(P A,P B) {return acos(Dot(A,B) / Length(A) / Length(B) ); }
friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10;
int dcmp(double x) {
if (fabs(x)<eps) return 0; else return x<0 ? -1 : 1;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
//Cross(v,w)==0(平行)时,不能调这个函数
P GetLineIntersection(P p,V v,P Q,V w){
V u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
P GetLineIntersectionB(P p,V v,P Q,V w){
return GetLineIntersection(p,v-p,Q,w-Q);
}
//点在线段上(不包含端点)
bool OnSegment(P p,P a1,P a2) {
return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<=0;
}
P read_point() {
P a;
scanf("%lf%lf",&a.x,&a.y);
return a;
}
bool IsParallel(P A,P B,P C,P D) {
return dcmp(Cross(B-A,D-C))==0;
}
int n;
P p1[600],p2[600];
bool flag[600][600];
P a[600][600];
set<double> dis[600];
set<double>::iterator it;
bool check(P A,P B,int i) {
double r1=Length(A-p1[i]),
r2=Length(B-p1[i]);
if (r1>r2) swap(r1,r2);
it = dis[i].upper_bound(r1);
while(!dcmp((*it)-r1)) it++;
return dcmp((*it)-r2)==0;
}
int main()
{
// freopen("uva1359.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
printf("Case %d:\n",kcase);
n=read();
Rep(i,n) p1[i]=read_point(),p2[i]=read_point();
MEM(flag)
Rep(i,n) {
Fork(j,i+1,n) if (!IsParallel(p1[i],p2[i],p1[j],p2[j])){
a[i][j] = GetLineIntersectionB(p1[i],p2[i],p1[j],p2[j]);
if (OnSegment(a[i][j],p1[i],p2[i])&&OnSegment(a[i][j],p1[j],p2[j]))
{
flag[i][j]=1;
dis[i].insert(Length(a[i][j] - p1[i] ) );
dis[j].insert(Length(a[i][j] - p1[j] ) );
}
}
dis[i].insert(0);
dis[i].insert(Length(p1[i]-p2[i]));
}
ll ans=0;
Rep(i,n) {
Fork(j,i+1,n-1) if (flag[i][j]){
Fork(k,j+1,n-1) if (flag[i][k] && flag[j][k]) {
P A=a[i][j],B=a[i][k],C=a[j][k];
if (A==B||A==C||B==C) continue;
ans += (check(A,B,i)&&check(A,C,j)&&check(B,C,k));
}
}
}
cout<<ans<<endl;
if (kcase<T) puts("");
Rep(i,n) dis[i].clear();
}
return 0;
}