HDU 1147 pick-up sticks (calculation of geometric judgment of straight line intersection)

Source: Internet
Author: User
Pick-up sticks

Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1682 accepted submission (s): 642

Problem descriptionstan 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.

Inputinput consists of a number of instances. the data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. the following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. the sticks are
Listed in the order in which Stan has thrown them. you may assume that there are no more than 1000 top sticks. the input is ended by the case with n = 0. this case shoshould not be processed.

Outputfor each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks shocould be listed in order in which they were thrown.
The picture to the right below without strates 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.

Sourceuniversity of Waterloo local contest 2005.09.17

The recommendeddy question is clearly about determining the standard intersection of a straight line. Let's take a look at the O (N ^ 2) Code and write it out, but it will time out, but it cannot be done either, it's still the original idea. It's okay to make a small optimization. This optimization is to record the points that are smaller than the current number and are not covered. In this way, a large number of redundant judgments can be omitted, because of the 100000 points, even if the CPU is idling for so many cycles, you will not be able to stand it!

# Include <iostream> # include <string. h> # include <math. h> # include <stdio. h >#include <algorithm> using namespace STD; # define EPS 1e-8struct point {Double X; Double Y ;}; struct line {point A; point B;} po [110000]; bool rec [110000]; int map [110000]; Double Multi (point P0, Point P1, point P2) // J calculate the variance multiplication {return (p1.x-distance X) * (p2.y-Policy)-(p2.x-p0.x) * (p1.y-policy);} bool is_cross (point & S1, point & E1, point & S2, point & E2) // determine whether a line segment is intersecting (non-normalized intersection) {return max (s1.x, e1.x)> min (s2.x, e2.x) & MAX (s2.x, e2.x)> min (s1.x, e1.x) & MAX (s1.y, e1.y)> min (s2.y, e2.y) & MAX (s2.y, e2.y)> min (s1.y, e1.y) & multi (S1, E1, s2) * multi (S1, E1, E2) <0 & multi (S2, E2, S1) * multi (S2, E2, E1) <0;} int main () {int N; int I, K, J, R; while (scanf ("% d", & N), n) {memset (REC, 1, sizeof (REC); for (I = 0; I <n; I ++) {scanf ("% lf", & po [I]. a. x, & po [I]. a. y, & po [I]. b. x, & po [I]. b. y); map [I] = I;} k = n-1; for (I = n-1; I> = 1; I --) {for (j = 0, r = 0; j <K; j ++) {If (REC [map [J] & map [J] <I) if (is_cross (po [I]. a, po [I]. b, po [map [J]. a, po [map [J]. b) Rec [map [J] = 0; else map [R ++] = map [J];} k = r;} printf ("top sticks: "); for (I = 0; I <n; I ++) if (REC [I]) {printf (" % d ", I + 1); break ;} I ++; for (I; I <n; I ++) if (REC [I]) printf (", % d", I + 1); printf (". \ n ") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.