Topic Link: Click to open the link
Pick-up Sticks Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2680 Accepted Submission (s): 978
Problem 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 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
5 1 1 4 2 2 3 3 1 1-2.0 8 4 1 4 8 2 3 3 6-2.0 3 0 0 1 1 1 0 2 1 2 0 3 1 0
Sample Output
Top Sticks:2, 4, 5. Top Sticks:1, 2, 3. Test instructions: Throw n sticks in turn to find the number of the top stick.
Idea: Enumerate sticks and find out the number of sticks that are not covered.
Code: The first 655ms, the second timeout. I think two should be about the same, all O (n²), may be the reason for the data.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
#define N 100010 struct line {double x1,y1,x2,y2;} Line[n];
struct Node {double x, y;};
int anss[n]; Double ans (Node A,node b) {return a.x*b.y-a.y*b.x;} int check (line A,line b) {if (! ( Min (a.x1,a.x2) <=max (b.x1,b.x2) &&min (a.y1,a.y2) <=max (b.y1,b.y2) && min (b.x1,b.x2) <=max (
A.X1,A.X2) &&min (b.y1,b.y2) <=max (a.y1,a.y2))) return 0;
Node P1q1,p2q1,q1p1,q2p1,q2q1,p2p1;
p1q1.x=a.x1-b.x1;
P1q1.y=a.y1-b.y1;
p2q1.x=a.x2-b.x1;
P2q1.y=a.y2-b.y1;
q1p1.x=b.x1-a.x1;
Q1p1.y=b.y1-a.y1;
q2p1.x=b.x2-a.x1;
Q2p1.y=b.y2-a.y1;
q2q1.x=b.x2-b.x1;
Q2q1.y=b.y2-b.y1;
p2p1.x=a.x2-a.x1;
P2p1.y=a.y2-a.y1;
if (ans (p1q1,q2q1) *ans (p2q1,q2q1) >0) return 0;
if (ans (q1p1,p2p1) *ans (Q2P1,P2P1) >0) return 0;
return 1;
} int main () {int n,cnt; while (~SCANF ("%d", &n) &&n) {cnt=0; for (int i=0; i<n; i++) scanf ("%lf%lf%lf%lf", &line[i].x1,&line[i].y1,&line[i].x2,&line[i]
. y2);
for (int i=0; i<n; i++) {int flag=1;
for (int j=i+1; j<n; J + +) if (check (Line[i],line[j])) {flag=0;
Break
} if (flag) anss[cnt++]=i+1;
} printf ("Top Sticks:");
printf ("%d", anss[0]);
for (int i=1; i<cnt; i++) printf (",%d", anss[i]);
printf (". \ n");
} return 0; }
</pre><pre style= "font-family: ' Courier New '; Background-color:rgb (244, 251, 255); " >
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std #define N 100010 struct line {double x1,y1,x2,y2;}
Line[n];
struct Node {double x, y;};
int flag[n]; Double ans (Node A,node b) {return a.x*b.y-a.y*b.x;} int check (line A,line b) {if (! ( Min (a.x1,a.x2) <=max (b.x1,b.x2) &&min (a.y1,a.y2) <=max (b.y1,b.y2) && min (b.x1,b.x2) <=max (a
. x1,a.x2) &&min (b.y1,b.y2) <=max (a.y1,a.y2))) return 0;
Node P1q1,p2q1,q1p1,q2p1,q2q1,p2p1;
p1q1.x=a.x1-b.x1;
P1q1.y=a.y1-b.y1;
p2q1.x=a.x2-b.x1;
P2q1.y=a.y2-b.y1;
q1p1.x=b.x1-a.x1;
Q1p1.y=b.y1-a.y1;
q2p1.x=b.x2-a.x1;
Q2p1.y=b.y2-a.y1;
q2q1.x=b.x2-b.x1;
Q2q1.y=b.y2-b.y1;
p2p1.x=a.x2-a.x1;
P2p1.y=a.y2-a.y1;
if (ans (p1q1,q2q1) *ans (p2q1,q2q1) >0) return 0;
if (ans (q1p1,p2p1) *ans (Q2P1,P2P1) >0) return 0;
return 1;
} int main () {int n; while (~SCANF ("%d", &amP;n) &&n) {memset (flag,0,sizeof (flag)); for (int i=0;i<n;i++) {scanf ("%lf%lf%lf%lf", &line[i].x1,&line[i].y1,&line[i].x2,&
; line[i].y2); for (int j=0;j<i;j++) {if (!flag[j]&&check (LINE[J],LINE[I))) FLA
G[j]=1;
}} int num=0;
printf ("Top Sticks:");
for (int i=0;i<n;i++) {if (!flag[i]) {if (num)
printf (",");
printf ("%d", i+1);
Num=1;
}} printf (". \ n");
} return 0; }