整理一下模板、才发现原来的写法是错的、郁闷。
HDU 1086
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7893 Accepted Submission(s): 3853
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define EPS 1e-8 #define PI acos(-1.0) int dump(double x) { if(fabs(x)<EPS) return 0; return x<0?-1:1; } struct Point { double x,y; Point (){} Point (double x,double y):x(x),y(y){} Point operator + (const Point &p)const { return Point(x+p.x,y+p.y); } Point operator - (const Point &p)const { return Point(x-p.x,y-p.y); } double operator ^ (const Point &p)const { return x*p.y-y*p.x; } double operator * (const Point &p)const { return x*p.x+y*p.y; } }; struct Line { Point s,e; Line (){} Line (Point s,Point e):s(s),e(e){} }; bool SegInterSeg(Line l1,Line l2) { return max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) && max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) && max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) && max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) && dump((l2.s-l1.e)^(l1.s-l1.e))*dump((l2.e-l1.e)^(l1.s-l1.e))<=0 && dump((l1.s-l2.e)^(l2.s-l2.e))*dump((l1.e-l2.e)^(l2.s-l2.e))<=0; } int main() { int i,j,n; Line l[1010]; while(scanf("%d",&n),n) { for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y); } int cnt=0; for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(SegInterSeg(l[i],l[j])) cnt++; } } cout<<cnt<<endl; } return 0; }
HDU 1147
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2172 Accepted Submission(s): 800
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define EPS 1e-8 #define N 100010 int dump(double x) { if(fabs(x)<EPS) return 0; return x<0?-1:1; } struct Point { double x,y; Point (){} Point (double x,double y):x(x),y(y){} Point operator + (const Point &p)const { return Point(x+p.x,y+p.y); } Point operator - (const Point &p)const { return Point(x-p.x,y-p.y); } double operator ^ (const Point &p)const { return x*p.y-y*p.x; } double operator * (const Point &p)const { return x*p.x+y*p.y; } }; struct Line { Point s,e; Line (){} Line (Point s,Point e):s(s),e(e){} }; bool SegInterSeg(Line l1,Line l2) { return max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) && max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) && max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) && max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) && dump((l2.s-l1.e)^(l1.s-l1.e))*dump((l2.e-l1.e)^(l1.s-l1.e))<=0 && dump((l1.s-l2.e)^(l2.s-l2.e))*dump((l1.e-l2.e)^(l2.s-l2.e))<=0; } int n,m; Line l[N]; int vis[N]; int main() { int i,j; while(scanf("%d",&n),n) { memset(vis,1,sizeof(vis)); for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(SegInterSeg(l[i],l[j])) { vis[i]=0; break; } } } printf("Top sticks: "); bool first=1; for(i=1;i<=n;i++) { if(vis[i]) { if(first) { first=0; cout<<i; } else cout<<", "<<i; } } cout<<'.'<<endl; } return 0; }
HDU 1558
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3564 Accepted Submission(s): 1328
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define EPS 1e-8 #define N 1010 int dump(double x) { if(fabs(x)<EPS) return 0; return x<0?-1:1; } struct Point { double x,y; Point (){} Point (double x,double y):x(x),y(y){} Point operator + (const Point &p)const { return Point(x+p.x,y+p.y); } Point operator - (const Point &p)const { return Point(x-p.x,y-p.y); } double operator ^ (const Point &p)const { return x*p.y-y*p.x; } double operator * (const Point &p)const { return x*p.x+y*p.y; } }; struct Line { Point s,e; Line (){} Line (Point s,Point e):s(s),e(e){} }; bool SegInterSeg(Line l1,Line l2) { return max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) && max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) && max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) && max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) && dump((l2.s-l1.e)^(l1.s-l1.e))*dump((l2.e-l1.e)^(l1.s-l1.e))<=0 && dump((l1.s-l2.e)^(l2.s-l2.e))*dump((l1.e-l2.e)^(l2.s-l2.e))<=0; } int n,m; int f[N]; Line l[N]; int sum[N]; int Find(int x) { return x==f[x]?x:Find(f[x]); } void un(int x,int y) { x=Find(x); y=Find(y); if(x>y) f[x]=y,sum[y]+=sum[x]; if(x<y) f[y]=x,sum[x]+=sum[y]; } int main() { int T,i,iCase=1; scanf("%d",&T); while(T--) { n=0; for(i=1;i<=1000;i++) { f[i]=i; sum[i]=1; } if(iCase!=1) printf("\n"); iCase++; scanf("%d",&m); while(m--) { char op; scanf(" %c",&op); if(op=='P') { n++; scanf("%lf%lf%lf%lf",&l[n].s.x,&l[n].s.y,&l[n].e.x,&l[n].e.y); for(i=1;i<n;i++) { if(SegInterSeg(l[n],l[i])) { un(n,i); } } } else { int x; scanf("%d",&x); x=Find(x); printf("%d\n",sum[x]); } } } return 0; }