Title Link: POJ 3168 Barn Expansion
Test instructions: Abstract is to give the coordinates of the n rectangle (the lower left and right corner of the coordinates, the edge of the rectangle is parallel to the X, Y axis), asked a few rectangles and other rectangles have no contact (there is only side contact or point contact, there is no public area).
Idea: Divide the sides into two categories, parallel x-axis and the y-axis. Sort the edges, and then for the first time, determine if there are any intersections.
AC Code:
#include <stdio.h> #include <vector> #include <map> #include <set> #include <algorithm> Using namespace std;struct node {int mark; int d,xx,yy; Node () {} node (int _d,int _xx,int _yy,int _mark) {D=_d,xx=_xx,yy=_yy,mark=_mark; }};vector<node> sx,sy;bool Vis[25010];bool CMP (node A,node b) {if (A.D!=B.D) return a.d<b.d; else if (a.xx!=b.xx) return a.xx<b.xx; else return a.yy<b.yy;} int main () {int n; int I,j,k;int a,b,c,d; while (scanf ("%d", &n)!=eof) {sx.clear (); Sy.clear (); memset (vis,false,sizeof vis); for (i=0; i<n; i++) {scanf ("%d%d%d%d", &a,&b,&c,&d); Sy.push_back (Node (b,a,c,i)); Sy.push_back (Node (d,a,c,i)); Sx.push_back (Node (a,b,d,i)); Sx.push_back (Node (c,b,d,i)); } int sz1,sz2; Sz1=sy.size (); Sz2=sx.size (); Sort (Sx.begin (), Sx.end (), CMP); Sort (Sy.begin (), Sy.end (), CMP); Vertical Y<yyint up;up=sy[0].yy; for (i=1;i<sz1;i++) {if (sy[i-1].d = = sy[i].d) {if (up >= sy[i].xx) {vis[sy[i].mark]=vis[sy[i-1].mark]=true;}} else Up=sy[i].yy;up=max (sy[i].yy,up); }up=sx[0].yy;for (i=1;i<sz2;i++) {if (sx[i-1].d = = sx[i].d) {if (up >= sx[i].xx) {Vis[sx[i].mark]=vis[sx[i-1].mark ]=true;}} else Up=sx[i].yy;up=max (sx[i].yy,up); }int ans=0;for (i=0;i<n;i++) {if (!vis[i]) ans++;} printf ("%d\n", ans); } return 0;} /*50 2 2 73 5 5 84 2 6 46 1 8 60 0 8 142 1 3 22 2 3 33 3 4 44 1 5 290 0 1 11 0 2 12 0 3 10 1 1 21 1 2 22 1 3 20 2 1 31 2 2 32 2 3 360 2 2 73 5 5 84 2 6 46 1 8 60 0 8 14 5 5 631 1 6 66 2 7 36 5 8 7*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3168 Barn Expansion (geometry + sort)