标签:ram arch pac ice class ring rate vector miss
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7324 | Accepted: 2936 |
Description
Input
Output
Sample Input
1 7 0 0 4 4 4 7 9 7 13 -1 8 -6 4 -4
Sample Output
80.00
Source
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; typedef long long ll; const int N=1505; const double INF=1e9; const double eps=1e-8; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } inline int sgn(double x){ if(abs(x)<eps) return 0; else return x<0?-1:1; } struct Vector{ double x,y; Vector(double a=0,double b=0):x(a),y(b){} bool operator <(const Vector &a)const{ return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0); } }; typedef Vector Point; Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);} Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);} Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);} Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);} bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;} double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;} double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} struct Line{ Point s,t; Line(){} Line(Point a,Point b):s(a),t(b){} }; bool isLSI(Line l1,Line l2){ Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s; return sgn(Cross(v,u))!=sgn(Cross(v,w)); } Point LI(Line a,Line b){ Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s; double t=Cross(v2,v)/Cross(v1,v2); return a.s+v1*t; } void iniPolygon(Point p[],int &n,double inf){ n=0; p[++n]=Point(inf,inf); p[++n]=Point(inf,-inf); p[++n]=Point(-inf,-inf); p[++n]=Point(-inf,inf); } Point t[N];int tn; void CutPolygon(Point p[],int &n,Point a,Point b){//get the left of a->b tn=0; Point c,d; for(int i=1;i<=n;i++){ c=p[i],d=p[i%n+1]; if(sgn(Cross(b-a,c-a))>=0) t[++tn]=c; if(isLSI(Line(a,b),Line(c,d))) t[++tn]=LI(Line(a,b),Line(c,d)); } n=tn;for(int i=1;i<=n;i++) p[i]=t[i]; } double PolygonArea(Point p[],int n){ double s=0; for(int i=2;i<n;i++) s+=Cross(p[i]-p[1],p[i+1]-p[1]); return abs(s/2); } int n,m; Point p[N],q[N]; int main(int argc, const char * argv[]){ int T=read(); while(T--){ n=read(); for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); iniPolygon(q,m,INF); for(int i=1;i<=n;i++) CutPolygon(q,m,p[i%n+1],p[i]); double ans=PolygonArea(q,m); printf("%.2lf\n",ans); } }
标签:ram arch pac ice class ring rate vector miss
原文地址:http://www.cnblogs.com/candy99/p/6358874.html