Topic Portal
Test instructions: is a game when I was a child, ask how many line segments are covered on the top
Analysis: Simple line segments intersect, queue maintains current top segment
/************************************************* author:running_time* Created time:2015/10/26 Monday 15:37:36* Fi Le Name:P oj_2653.cpp ************************************************/#include <cstdio> #include < algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath># Include <string> #include <vector> #include <queue> #include <deque> #include <stack># Include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Double EPS = 1e-10;c Onst Double PI = ACOs ( -1.0), int dcmp (double x) {//three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;} struct Point {//points defined by double x, y; Point(Double x=0, double y=0): X (x), Y (y) {} point operator + (const-point &r) const {//vector addition return Poi NT (x + r.x, y + r.y); } Point operator-(const point &r) const {//Vector subtraction return point (x-r.x, Y-R.Y); } Point operator * (double p) {///vector multiplied by a scalar return point (x * p, Y * p); } point operator/(double p) {///vector divided by scalar return point (x/p, y/p); } BOOL operator < (const point &r) const {//DOT coordinate sort return x < R.x | | (x = = r.x && y < r.y); } bool operator = = (Const point &r) const {//judgment same dot return dcmp (x-r.x) = = 0 && dcmp (y-r . y) = = 0; }};typedef Point Vector; Vector definition point read_point (void) {//points read in double x, y; scanf ("%lf%lf", &x, &y); Return point (x, y);} Double dot (vector A, vector B) {//vector dot product return a.x * b.x + a.y * B.Y;} Double Cross (vector A, vector B) {//vector fork product return A.X * b.y-a.y * b.x;} Double Polar_angle (vector A) {//Vector polar return atan2 (A.Y, a.x);} Double length (vector a) {//vector length, dot product return sqrt (dot (A, a));} Double angle (vector a, vector b) {//Vector corner, counterclockwise, dot product return ACOs (dot (A, b)/Length (a)/length (b));} Vector Rotate (vector A, double rad) {//vector rotation, counter-clockwise return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad ) + a.y * cos (RAD));} Vector Nomal (vector a) {//vector double len = length (a) of the unit method vectors; Return Vector (-a.y/len, A.x/len);} Point Line_line_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q; Double T = Cross (w, U)/Cross (V, W); return p + V * t;} Double Point_to_line the distance from {//points to a straight line, two-point Vector V1 = b-a, V2 = p-a; Return Fabs (Cross (V1, V2))/Length (V1);} Double point_to_seg the distance from {//points to a line segment, two-point if (a = = b) return length (P-A); Vector V1 = b-a, V2 = P-a,V3 = P-b; if (DCMP (dot (V1, V2)) < 0) return length (V2); else if (dcmp (dot (V1, V3)) > 0) return length (V3); else return Fabs (cross (V1, V2))/Length (V1);} Point Point_line_proj (point P, points a, dot b) {//dots are projected on a line, two points Vector V = b-a; Return a + v * (dot (v, p-a)/dot (V, v));} BOOL Can_inter (Points A1, point A2, Spot b1, dot b2) {//Judgment segment intersection, two-bit double C1 = Cross (A2-A1, b1-a1), C2 = Cross (A2-A1, b2-a1), C3 = Cross (B2-B1, a1-b1), C4 = Cross (b2-b1, A2-B1); Return DCMP (C1) * DCMP (C2) < 0 && dcmp (C3) * DCMP (C4) < 0;} BOOL On_seg (point P, bit A1, points A2) {//Judgment dot on segment, two-point return dcmp (Cross (a1-p, a2-p)) = = 0 && DCMP (dot (a1-p, a2-p)) < 0;} Double Area_triangle (Point A, point B, point C) {//triangular area, fork product return Fabs (Cross (b-a, c-a))/2.0;} Double Area_poly (point *p, int n) {//Polygon area, cross product double ret = 0; for (int i=1; i<n-1; ++i) {ret + = Fabs (Cross (P[i]-p[0], p[i+1]-p[0])); } return RET/2;} /* Point set Convex package */vector<point> convex_hull (vector<point> &p) {sort (P.begin (), P.end ()); int n = p.size (), k = 0; vector<point> ret (n * 2); for (int i=0; i<n; ++i) {when (K > 1 && Cross (ret[k-1]-ret[k-2], P[i]-ret[k-1]) <= 0) k--; ret[k++] = P[i]; } for (int i=n-2, t=k; i>=0;-I.) {while (K > t && Cross (Ret[k-1]-ret[k-2], P[i]-ret[k-1]) <= 0) k--; ret[k++] = P[i]; } ret.resize (k-1); return ret;} struct Circle {point C; Double R; Circle () {} circle (point C, Double R): C (c), R (r) {}-point-point (double a) {return-point (c.x + cos ( A) * R, C.y + sin (a) * R); }};struct Line {point P; Vector v; Double R; Line () {} line (const point &p, const Vector &v): P (P), V (v) {r = Polar_angle (v); } Point PoiNT (double A) {return P + v * A; }}; Point S[n], e[n];bool vis[n];int main (void) {int N; while (scanf ("%d", &n) = = 1) {if (!n) break; Memset (Vis, true, sizeof (VIS)); for (int i=1; i<=n; ++i) {S[i] = Read_point (); E[i] = Read_point (); } queue<int> Q; Q.push (1); for (int i=2; i<=n; ++i) {Q.push (i); while (! Q.empty ()) {if (i = = Q.front ()) break; Int J = Q.front (); Q.pop (); if (Can_inter (S[i], e[i], s[j], e[j])) {Vis[j] = false; } else Q.push (j); }} printf ("Top Sticks:"); bool Fir = true; for (int i=1; i<=n; ++i) {if (Vis[i]) {if (FIR) {printf ("%d", i) ; FIR = false; } else {printf (",%d", I); }}} puts ("."); }//cout << "time Elapsed:" << 1.0 * Clock ()/clocks_per_sec << "s.\n"; return 0;}
Simple geometry (segment intersection) POJ 2653 Pick-up Sticks