标签:ring ota 断线 暴力 bre nat com output 线段相交
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 12861 | Accepted: 4847 |
Description
Input
Output
Sample Input
5 1 1 4 2 2 3 3 1 1 -2.0 8 4 1 4 8 2 3 3 6 -2.0 3 0 0 1 1 1 0 2 1 2 0 3 1 0
Sample Output
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.
Hint
Source
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; typedef long long ll; const int N=1e5+5; 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 x<a.x||(x==a.x&&y<a.y); } void print(){ printf("%lf %lf\n",x,y); } }; 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 Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } double Dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double DisPP(Point a,Point b){ Point t=a-b; return sqrt(t.x*t.x+t.y*t.y); } struct Line{ Point s,t; Line(){} Line(Point p,Point v):s(p),t(v){} }l[N]; 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)); } bool isSSI(Line l1,Line l2){ Vector v1=l1.t-l1.s,v2=l2.t-l2.s; if(sgn(Cross(v1,v2))==0){ int flag=0; Vector u=l2.s-l1.s,w=l2.t-l1.s; if(sgn(Dot(u,w))<0) flag=1; u=l2.s-l1.t,w=l2.t-l1.t; if(sgn(Dot(u,w))<0) flag=1; return flag; } else return isLSI(l1,l2)&&isLSI(l2,l1); } int n; bool vis[N]; double x,y,x2,y2; int main(int argc, const char * argv[]) { while(true){ memset(vis,0,sizeof(vis)); n=read(); if(n==0) break; for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2); l[i]=Line(Point(x,y),Point(x2,y2)); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++) if(isSSI(l[j],l[i])){vis[i]=1;break;} } printf("Top sticks: "); int fir=1; for(int i=1;i<=n;i++) if(!vis[i]){ if(fir) printf("%d",i),fir=0; else printf(", %d",i); } puts("."); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; typedef long long ll; const int N=1e5+5; 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 x<a.x||(x==a.x&&y<a.y); } void print(){ printf("%lf %lf\n",x,y); } }; 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 Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } double Dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double DisPP(Point a,Point b){ Point t=a-b; return sqrt(t.x*t.x+t.y*t.y); } struct Line{ Point s,t; Line(){} Line(Point p,Point v):s(p),t(v){} }l[N]; 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)); } bool isSSI(Line l1,Line l2){ Vector v1=l1.t-l1.s,v2=l2.t-l2.s; if(sgn(Cross(v1,v2))==0){ int flag=0; Vector u=l2.s-l1.s,w=l2.t-l1.s; if(sgn(Dot(u,w))<0) flag=1; u=l2.s-l1.t,w=l2.t-l1.t; if(sgn(Dot(u,w))<0) flag=1; return flag; } else return isLSI(l1,l2)&&isLSI(l2,l1); } int n,st[N],top; inline void del(int p){ for(int i=p;i<=top;i++) st[i]=st[i+1];top--; } double x,y,x2,y2; int main(int argc, const char * argv[]) { while(true){ top=0; n=read(); if(n==0) break; for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2); l[i]=Line(Point(x,y),Point(x2,y2)); for(int j=1;j<=top;j++) if(isSSI(l[st[j]],l[i])) del(j),j--; st[++top]=i; } printf("Top sticks: %d",st[1]); for(int i=2;i<=top;i++) printf(", %d",st[i]); puts("."); } return 0; }
POJ 2653 Pick-up sticks [线段相交 迷之暴力]
标签:ring ota 断线 暴力 bre nat com output 线段相交
原文地址:http://www.cnblogs.com/candy99/p/6353941.html