Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.
分析:
解題思路比較簡單——每放一根棍子,都判斷一下它與它前面的且在頂端的棍子是否相交,相交的話則將相應的棍子從解空間中除去(當前這根暫時是在解空間中的)
但是,如果直接用數組做會超時,然後用鏈表做(剛開始自己寫了個鏈表,但是寫壞掉了,哎,基礎啊……),於是最後還是用了list
除數據結構外,最主要的算法就是判斷兩直線相交了,直接用了模板
代碼:
//hdu 1147 #include#include #include #include #include #include #include using namespace std; const double eps=1e-8; int sgn(double x) { if(fabs(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) && sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0 && sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0 ; } int main() { freopen("in.txt","r",stdin); int n; int len; node cur; list stick; list ::iterator p; while(scanf("%d",&n),n){ for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&cur.t.s.x,&cur.t.s.y,&cur.t.e.x,&cur.t.e.y); cur.no=i; stick.push_back(cur); if(i>1){ p=stick.begin(); len=stick.size(); for(int i=1;i