POJ2318 TOYS + 2398 Toy Storage
二分+叉积
http://poj.org/problem?id=2318
http://poj.org/problem?id=2398
//2318
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const double eps=1e-8; const int maxn=100004; int dcmp(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } #define Point Vector struct Vector{ double x,y; Vector(double x=0,double y=0):x(x),y(y){}; Vector operator + (const Vector &A) const{ return Vector(x+A.x,y+A.y); } Vector operator - (const Vector &A) const{ return Vector(x-A.x,y-A.y); } Vector operator * (const double &A) const{ return Vector(x*A,y*A); } Vector operator / (const double &A) const{ return Vector(x/A,y/A); } bool operator == (const Vector &A) const{ return dcmp(x-A.x)==0&&dcmp(y-A.y)==0; } }; struct Line{ Point p1,p2; Line(){}; Line(Point p1,Point p2):p1(p1),p2(p2){} }li[maxn]; int ans[maxn],n,m; double U,L,R,D; double PolarAngle(Vector A){ return atan2(A.y,A.x); } Vector rotate(Vector &A,double a){ return A=Vector(A.x*cos(a)-A.y*sin(a),A.x*sin(a)+A.y*cos(a)); } 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; } bool pointisleft(Point p,Line line) { Vector v1=line.p1-p; Vector v2=line.p2-p; return dcmp(Cross(v1,v2))<0; } int main(){ while(scanf("%d",&n)!=EOF){ if(n==0) return 0; scanf("%d",&m); scanf("%lf%lf%lf%lf",&L,&U,&R,&D); //Point p1=Point(L,U); //Point p2=Point(R,D); //li[0]=Line(p1,p2); for(int i=0;i<n;i++){ double x1,x2; scanf("%lf%lf",&x1,&x2); Point p1=Point(x1,U); Point p2=Point(x2,D); li[i]=Line(p1,p2); } memset(ans,0,sizeof(ans)); for(int i=1;i<=m;i++){ Point p;scanf("%lf%lf",&p.x,&p.y); int l=0,r=n-1; while(l<=r){ int mid=l+r>>1; if(Cross(li[mid].p1-li[mid].p2,p-li[mid].p2)>0) r=mid-1; //if(pointisleft(p,li[mid])) r=mid-1;//两种写法 else l=mid+1; } ans[l]++; } for(int i=0;i<=n;i++){ printf("%d: %d\n",i,ans[i]); } printf("\n"); } }
//2398
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const double eps=1e-8; const int maxn=100004; int dcmp(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } #define Point Vector struct Vector{ double x,y; Vector(double x=0,double y=0):x(x),y(y){}; Vector operator + (const Vector &A) const{ return Vector(x+A.x,y+A.y); } Vector operator - (const Vector &A) const{ return Vector(x-A.x,y-A.y); } Vector operator * (const double &A) const{ return Vector(x*A,y*A); } Vector operator / (const double &A) const{ return Vector(x/A,y/A); } bool operator == (const Vector &A) const{ return dcmp(x-A.x)==0&&dcmp(y-A.y)==0; } }; struct Line{ Point p1,p2; Line(){}; Line(Point p1,Point p2):p1(p1),p2(p2){} }li[maxn]; bool operator < (Line l1,Line l2){ if(l1.p1.x==l2.p1.x) return l1.p1.y<l2.p1.y; else return l1.p1.x<l2.p1.x; } int ans[maxn],num[maxn],n,m; double U,L,R,D; double PolarAngle(Vector A){ return atan2(A.y,A.x); } Vector rotate(Vector &A,double a){ return A=Vector(A.x*cos(a)-A.y*sin(a),A.x*sin(a)+A.y*cos(a)); } 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; } bool pointisleft(Point p,Line line) { Vector v1= line.p1-p; Vector v2= line.p2-p; //cout<<line.p1.x<<' '<<line.p1.y<<' '<<line.p2.x<<' '<<line.p2.y<<' '<<p.x<<' '<<p.y<<endl; //cout<<v1.x<<' '<<v1.y<<' '<<v2.x<<' '<<v2.y<<endl; //cout<<Cross(v1,v2)<<endl; return dcmp(Cross(v1,v2))<0; } int main(){ while(scanf("%d",&n)!=EOF){ if(n==0) return 0; scanf("%d",&m); scanf("%lf%lf%lf%lf",&L,&U,&R,&D); //Point p1=Point(L,U); //Point p2=Point(R,D); //li[0]=Line(p1,p2); for(int i=0;i<n;i++){ double x1,x2; scanf("%lf%lf",&x1,&x2); Point p1=Point(x1,U); Point p2=Point(x2,D); li[i]=Line(p1,p2); } sort(li,li+n); memset(ans,0,sizeof(ans)); memset(num,0,sizeof(num)); for(int i=1;i<=m;i++){ Point p;scanf("%lf%lf",&p.x,&p.y); int l=0,r=n-1; while(l<=r){ int mid=l+r>>1; if(Cross(li[mid].p1-li[mid].p2,p-li[mid].p2)>0) r=mid-1; //cout<<pointisleft(p,li[mid])<<endl; //if(pointisleft(p,li[mid])) r=mid-1; else l=mid+1; } ans[l]++; } for(int i=0;i<=n;i++){ num[ans[i]]++; } printf("Box\n"); for(int i=1;i<=m;i++){ if(num[i]) printf("%d: %d\n",i,num[i]); } //printf("\n"); } }