1+ // This is cowjump version 2 aka Cow Steeplechase USACO US Open Silver problem 2
2+
13import java .io .*;
24import java .util .*;
35public class cowjump {
4- public static int pointYCompare ( Point p1 , Point p2 ) {
5- // Compare points
6- // Check if p2 is above, below, or next to p1
7- System . out . println ( "pointycompare((" + p1 . x + "," + p1 . y + "),(" + p2 . x + "," + p2 . y + ")" );
8- if ( p1 . y == p2 . y ) {
9- System . out . println ( "output: 0" ); // Same Y
10- return 0 ;
11- }
12- if ( p2 . y < p1 . y ) {
13- System . out . println ( "output: -1" ); // below
14- return - 1 ;
6+ static ArrayList < Point > points ;
7+ static ArrayList < LineSegement > segements ;
8+ static int N ;
9+
10+ public static Point op ( int i , Point k , Point [][] input ) {
11+ Point a = input [ i ][ 0 ];
12+ Point b = input [ i ][ 0 ] ;
13+ if ( a . eq ( k )) {
14+ return b ;
15+ } else {
16+ return a ;
1517 }
16- if (p2 .y > p1 .y ) {
17- System .out .println ("output: 1" ); // above
18+ }
19+ public static int num (int i ,Point k , Point [][] input ) {
20+ Point a = input [i ][0 ];
21+ Point b = input [i ][1 ];
22+ //System.out.println(a +" -- "+b);
23+ if (a .eq (k )) {
24+ //System.out.println("!");
25+ return 0 ;
26+ }else {
1827 return 1 ;
1928 }
20- System .out .println ("NONE OF THE ABOVE" ); // This code should never run, but just keeping this to prevent syntax errors
21- return -9999999 ;
22- }
23- public static int linesCompare (LineSegement m , LineSegement l ) {
24- System .out .println ("Checking if line (" +m .a .x +"," +m .a .y +")--" +"(" +m .b .x +"," +m .b .y +")" + " intersects with " + "(" +l .a .x +"," +l .a .y +")--" +"(" +l .b .x +"," +l .b .y +")" );
25- return pointYCompare (m .a , l .a ) * pointYCompare (m .b , l .b );
26- }
27- public static void testIntersections () {
28- assert Point .intersection (new Point (0 ,0 ), new Point (2 ,9 ), new Point (0 ,1 ), new Point (6 ,1 ))== true ;
29- //assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false;
30- assert linesCompare (new LineSegement (new Point (0 ,0 ), new Point (2 ,3 )),new LineSegement (new Point (0 ,3 ),new Point (9 ,1 ))) == -1 ;
31- System .out .println ("All Tests OK!" );
32- }
33- public static boolean sweepCheck (LineSegement s ,Point [][] input ) {
34- for (int i = 0 ; i < input .length ; i ++) {
35- System .out .println ("Checking line " +i );
36- if (input [i ][0 ] == null || input [i ][1 ] == null ) {
37- System .out .println ("End of segments" );
38- break ;
39- }
40- System .out .println ("Checking " +s .a .x +" - " +input [i ][0 ].x + " - " +s .b .x );
41- boolean firstWithinLine = (s .a .x <= input [i ][0 ].x && s .b .x >= input [i ][0 ].x );
42- System .out .println ("firstWithinLine = " +firstWithinLine );
43- //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
44- if (firstWithinLine ) {
45- // TODO check line cross logic
46- if (linesCompare (s ,new LineSegement (input [i ][0 ], input [i ][1 ])) == -1 ) {
47- System .out .println ("Intersect!" );
48- return true ;
49- }else {
50- System .out .println ("No Intersection!" );
51- }
52- continue ; // Both statements may be true
53- }
54- boolean secondWithinLine = (s .a .x <= input [i ][1 ].x && s .b .x >= input [i ][1 ].x );
55- //|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
56- if (secondWithinLine ) {
57- // TODO check line cross logic
58- if (linesCompare (s ,new LineSegement (input [i ][0 ], input [i ][1 ])) == -1 ) {
59- System .out .println ("Intersect!" );
60- return true ;
61- }else {
62- System .out .println ("No Intersection!" );
63- }
64- }
65-
29+ }
30+ public static boolean edge (int i , Point k , Point [][] input ) {
31+ if (input [i ][1 ].x == k .x && input [i ][0 ].x < k .x ) {
32+ return true ;
33+ }
34+ else if (input [i ][0 ].x == k .x && input [i ][1 ].x < k .x ) {
35+ return true ;
6636 }
6737 return false ;
6838 }
6939 public static void main (String [] args ) throws IOException {
70- testIntersections ();
71- // TODO Auto-generated method stub
72- //testIntersections();
40+
41+ // File Openning
7342 BufferedReader f = new BufferedReader (new FileReader ("cowjump.in" ));
74- int N = Integer .parseInt (f .readLine ());
75- Point [][] input = new Point [N ][2 ];
76- //System.out.println(input[0][0]);
77- for (int i = 0 ; i < N ; i ++) {
43+ points = new ArrayList <Point >();
44+ segements = new ArrayList <LineSegement >();
45+ N = Integer .parseInt (f .readLine ());
46+ Point [][] lookup = new Point [N ][2 ];
47+ for (int i =0 ; i < N ; i ++) {
48+ Point a ,b ;
7849 StringTokenizer st = new StringTokenizer (f .readLine ());
79- Point a = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
80- Point b = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
50+ a = new Point (Integer .parseInt (st .nextToken ()),Integer .parseInt (st .nextToken ()));
51+ b = new Point (Integer .parseInt (st .nextToken ()),Integer .parseInt (st .nextToken ()));
52+ a = a .setIndex (i );
53+ b = b .setIndex (i );
8154 if (a .x > b .x ) {
82- System .out .println (sweepCheck (
83- new LineSegement (b ,a ),
84- input ));
55+ lookup [i ][0 ] = b ;
56+ lookup [i ][1 ] = a ;
8557 }else {
86- System .out .println (sweepCheck (
87- new LineSegement (a ,b ),
88- input ));
58+ lookup [i ][0 ] = a ;
59+ lookup [i ][1 ] = b ;
8960 }
90- input [i ][0 ] = a ;
91- input [i ][1 ] = b ;
92- /*for(int j = 0; j < i; j ++) {
93- if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) {
94- PrintWriter pw = new PrintWriter("cowjump.out");
95- pw.println(i+1);
96- pw.close();
97- System.exit(0);
98- }
99- }*/
61+ points .add (a );
62+ points .add (b );
63+ segements .add (new LineSegement (a , b ));
10064 }
65+ points .sort (null );
66+ // Algorthim
67+ int size = points .size ();
10168
69+ for (int i = 0 ; i < size ; i ++) {
70+ //System.out.println(points.get(i));
71+ }
72+ for (int i =0 ; i < N ; i ++) {
73+ //System.out.println(Arrays.toString(lookup[i]));
74+ //System.out.println(segements.get(i));
75+ }
76+
77+ int currentY = -1 ;
78+ int index ;
79+ // boolean newY = true;
80+ List <Integer > pz = new ArrayList <Integer >();
81+ int [] tbl = new int [N ];
82+ int max = -1 ;
83+ int maxi = -1 ;
84+ for (int i = 0 ; i < N ; i ++) {
85+ Point p = points .get (i );
86+ //index = p.index;
87+ /*if(p.y != currentY) {
88+ currentY = (int) p.y;
89+ continue;
90+ }else {
91+ */
92+ int state = num (p .index ,p ,lookup );
93+ //System.out.println("Endpoint "+ state + " "+p);
94+ if (state == 0 ) {
95+ pz .add (p .index );
96+ }else if (state == 1 ) {
97+ pz .remove (pz .indexOf (p .index ));
98+
99+ }else {
100+ continue ;
101+ }
102+ int sz = pz .size ();
103+ for (int j =0 ;j < sz ; j ++ ) {
104+ for (int k = j + 1 ; k < sz ; k ++) {
105+ //if(j == k){
106+ // continue;
107+ //}
108+ if (Point .intersection (segements .get (j ),segements .get (k ))) {
109+ //System.out.println("Intersectsion at "+segements.get(j)+" -|||- "+segements.get(k));
110+ tbl [i ] ++;
111+ tbl [j ] ++;
112+ if (tbl [i ] > max ) {
113+ max = tbl [i ];
114+ maxi = i ;
115+ }
116+ if (tbl [j ] > max ) {
117+ max = tbl [j ];
118+ maxi = j ;
119+ }
120+ }
121+ }
122+ }
123+ //}
124+ }
125+ // File Writting
126+ //System.out.println(max + " " + maxi + " " + Arrays.toString(tbl));
127+ PrintWriter pw = new PrintWriter (new BufferedWriter (new FileWriter ("cowjump.out" )));
128+ pw .println (max + 1 );
129+ pw .close ();
102130 }
103131
104132}
105- class Point {
106- int x ,y ;
133+ class Point implements Comparable <Point >{
134+ double x ,y ;
135+ int index = -1 ;
136+ public Point (double x ,double y ) {
137+ this .x = x ;
138+ this .y = y ;
139+ }
107140 public Point (int x ,int y ) {
108141 this .x = x ;
109142 this .y = y ;
@@ -112,7 +145,12 @@ public Point() {
112145 this .x = 0 ;
113146 this .y = 0 ;
114147 }
148+ public Point setIndex (int i ) {
149+ this .index = i ;
150+ return this ;
151+ }
115152 static boolean intersection (Point a , Point b ,Point c , Point d ) {
153+ // OLD CALCULATION CODE
116154 Point E = new Point (b .x - a .x , b .y - a .y );
117155 Point F = new Point (d .x - c .x , d .y - c .y );
118156 Point P = new Point (-E .y , E .x );
@@ -122,15 +160,36 @@ static boolean intersection(Point a, Point b,Point c, Point d) {
122160 // Parallel
123161 return false ;
124162 }
125-
126163 double h = (Q .x * P .x + Q .y * P .y )/(k );
127164 if (0 <= h && h <= 1 ) {
128165 return true ;
129166 }
130167 return false ;
131168 }
169+ static Set <Pair <LineSegement ,LineSegement >> notwice = new HashSet <Pair <LineSegement ,LineSegement >>();
170+ static boolean intersection (LineSegement a ,LineSegement b ) {
171+ if (notwice .contains (new Pair <LineSegement ,LineSegement >(a ,b ))) {
172+ return false ;
173+ }else {
174+ notwice .add (new Pair <LineSegement ,LineSegement >(a ,b ));
175+ }
176+ return intersection (a .a , a .b , b .a , b .b );
177+ }
178+ public String toString () {
179+ return "| (" +this .x + "," + this .y + ") INDEX " +this .index + " |" ;
180+ }
181+ boolean eq (Point q ) {
182+ if (q .x == this .x && q .y == this .y ) {
183+ return true ;
184+ }
185+ return false ;
186+ }
187+ @ Override
188+ public int compareTo (Point arg0 ) {
189+ return Double .compare (this .x , arg0 .x );
190+ //return 0;
191+ }
132192}
133-
134193class LineSegement {
135194 Point a ,b ;
136195 public LineSegement (Point a ,Point b ) {
@@ -141,12 +200,66 @@ public LineSegement(Point a,Point b) {
141200 this .a = a ;
142201 this .b = b ;
143202 }
203+
204+ }
205+ public String toString () {
206+ return "{" +this .a +"," +this .b +"}" ;
144207 }
145- public double atX (int x ) {
208+ public double atX (double x ) {
146209 if (this .a .y == this .b .y ) { // Straight
147210 return this .a .y ;
148211 }else {
149212 return this .a .y * (x /this .a .x );
150213 }
151214 }
215+
216+ public Point atX_ (double x ) {
217+ return new Point (x ,this .atX (x ));
218+ }
219+ /*
220+ public String toString() {
221+ return this.a.toString() + " -- "+this.b.toString();
222+ }
223+ */
224+ }
225+ class Pair <F , S > {
226+ private F first ; //first member of pair
227+ private S second ; //second member of pair
228+
229+ public Pair (F first , S second ) {
230+ this .first = first ;
231+ this .second = second ;
232+ }
233+
234+ public void setFirst (F first ) {
235+ this .first = first ;
236+ }
237+
238+ public void setSecond (S second ) {
239+ this .second = second ;
240+ }
241+
242+ public F getFirst () {
243+ return first ;
244+ }
245+ @ Override
246+ public boolean equals (Object obj ){
247+ if (obj instanceof Pair ) {
248+ //Pair<LineSegement, LineSegement> p = (Pair<LineSegement, LineSegement>) obj;
249+ //Pair<LineSegement, LineSegement> m = (Pair<LineSegement, LineSegement>) this;
250+ //return (p.first.a.x == m.first.a.x) && (p.first.a.x == m.first.a.y) && (p.first.b.x == m.first.b.x) && (p.first.b.x == m.first.b.y) && (p.second.a.x == m.second.a.x) && (p.second.a.x == m.second.a.y) && (p.second.b.x == m.second.b.x) && (p.second.b.x == m.second.b.y);
251+ Pair q = (Pair ) obj ;
252+ return q .first .equals (this .first ) && q .second .equals (this .second );
253+ }else {
254+ return false ;
255+ }
256+ }
257+ @ Override
258+ public int hashCode () {
259+ return first .hashCode () * second .hashCode ();
260+ }
261+ public S getSecond () {
262+ return second ;
263+ }
264+
152265}
0 commit comments