http://poj.org/problem?id=2653
I am curious why this $o (n^2) $ violence can be too ....
Although this is added to the list optimization, but the worst is not $o (n^2) $ ... (can only say that the data is too weak ...) )
Then the subject of the bare sentence line intersection and point in line ... (See the online standard, do not judge the end of the situation can pass I was drunk ...) )
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long long ll; #define PII pair<int, int> #define MKPII make_pair<int, int> #define PDI Pair<double, in T> #define MKPDI make_pair<double, int> #define PLI pair<ll, int> #define MKPLI make_pair<ll, int># Define REP (I, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define FOR2 (i,a,n) for (int i= (a);i< (n); ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= (a);i> (n); Define CC (I,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) #define DBG (x) cout <& Lt (#x) << "=" << (x) << endl#define error (x) (!) ( x) puts ("error"): 0) #define PRINTARR2 (A, B, c) For1 (_, 1, b) {For1 (__, 1, c) cout << a[_][__]; cout << Endl } #define PRINTARR1 (A, B) For1 (_, 1, b) cout << a[_] << ' \ t '; cout << endlinline const int Getint () {int r=0, k=1; char C=getchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }inline const int MAX (const int &a, const int &b) {return a>b?a:b;} inline const int min (const int &A, const int &b) {return a<b?a:b;} Const double Eps=1e-6;int dcmp (double x) {return abs (x) <eps?0: (x<0?-1:1); }struct point {double x, y; Point (Double _x=0, double _y=0): X (_x), Y (_y) {}};typedef point Vector; Vector operator-(Point &a, point &b) {return vector (a.x-b.x, a.y-b.y);} BOOL operator== (Point &a, point &b) {return dcmp (a.x-b.x) ==0 && dcmp (A.Y-B.Y) ==0;} Double Cross (vector a, vector b) {return a.x*b.y-b.x*a.y;} Double Dot (vector A, vector b) {return a.x*b.x+a.y*b.y;} int Ssjiao (Point p1, point P2, point Q1, point Q2) {return (DCMP (CRoss (P1-Q1, Q2-Q1)) ^dcmp (Cross (P2-Q1, q2-q1)) ==-2 && (dcmp (Q1-P1, P2-P1)) ^dcmp (Cross (Q2-P1, p2-p1)) = =-2;} int Onsegment (Point A, point B, point C) {if (A==b | | a==c) return-1;if (dcmp (b, c-b) ==0 && dcmp (Dot (B-a, c-a)) ==-1) return 1;return 0; }const int N=100015;int nxt[n], ans[n]; Point A[n][2];bool Check (int. now, int goal) {if (Onsegment (a[now][0], a[goal][0], a[goal][1]) | | Onsegment (A[now][1], a[goal][0], a[goal][1]) | | Onsegment (A[goal][0], a[now][0], a[now][1]) | | Onsegment (A[goal][1], a[now][0], a[now][1])) return 1; if (Ssjiao (a[now][0], a[now][1], a[goal][0], a[goal][1])) return 1;return 0;} void del (int f, int now) {Nxt[f]=nxt[now];} void work (int goal) {int now=nxt[0], fa=0;while (now!=goal) {if (check (now, goal)) del (FA, now); else fa=now;now=nxt[now];}} int main () {int n;while (read (n), N) {Nxt[0]=1;for1 (i, 1, N) nxt[i]=i+1;for1 (i, 1, N) {rep (k, 2) scanf ("%lf%lf", &a[i][ k].x, &A[I][K].Y); work (i);} printf ("Top Sticks:"); int now=0, CNt=0;while (nxt[now]!=n+1) {Ans[++cnt]=nxt[now];now=nxt[now];} For1 (i, 1, cnt-1) printf ("%d,", ans[i]); printf ("%d.\n", ans[cnt]);} return 0;}
Description Stan has n sticks of various length. He throws them one at a time to the floor in a random. After finishing throwing, Stan tries to find the top sticks, that's these sticks such that there was no stick on top of th Em. Stan has noticed, the last thrown stick was always on top and he wants to know all the sticks that's on top. Stan sticks is very, very thin such that their thickness can be neglected.Input Input consists of a number of cases. The data for each case is start with 1 <= n <= 100000, and the number of the sticks for this case. The following n lines contain four numbers each, these numbers is the planar coordinates of the endpoints of one stick. The sticks is listed in the order in which Stan had thrown them. Assume that there is no more than top sticks. The input is ended by the case with N=0. This case is should not being processed.Output For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should is listed in order in which they were thrown.
The below illustrates the first case from input.Sample Input 51 1 4 22 3 3 11-2.0 8 41 4 8 23 3 6-2.030 0 1 11 0 2 12 0 3 10
Sample Output Top Sticks:2, 4, 5.Top sticks:1, 2, 3.
Hint Huge input,scanf is recommended.Source Waterloo Local 2005.09.17 |
"POJ" 2653 Pick-up sticks (computational geometry + violence)