hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1872 Accepted Submission(s): 706
Problem Description
Stan 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.
Input
Input consists of a number of cases. 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 should not be 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 be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
The picture to the right 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.
Source
Recommend
计算几何,判断两线段相交。
题意是依次放置线段,相交即摞在上面,求最后在最上面的线段。
急躁了,思路彻底乱了,最后还是参考了别人的代码。比赛的时候如果是这种状态,那绝对会拖累队友。
1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 using namespace std;
5
6 struct Point{
7 double x,y;
8 };
9 struct Line{
10 Point p1,p2;
11 }l[100010];
12 double Max(double a,double b)
13 {
14 return a>b?a:b;
15 }
16 int MaxInt(int a,int b)
17 {
18 return a>b?a:b;
19 }
20 double Min(double a,double b)
21 {
22 return a<b?a:b;
23 }
24 double xmulti(Point p1,Point p2,Point p0)
25 {
26 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
27 }
28 bool inter(Line l1,Line l2)
29 {
30 if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
31 Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
32 Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
33 Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
34 xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<0 &&
35 xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<0 )
36 return true;
37 else
38 return false;
39 }
40 bool isv[100010];
41 int main()
42 {
43 int n;
44 while(cin>>n){
45 if(n==0) break;
46 memset(isv,0,sizeof(isv));
47 for(int i=1;i<=n;i++){
48 scanf("%lf%lf%lf%lf",&l[i].p1.x,&l[i].p1.y,&l[i].p2.x,&l[i].p2.y);
49 }
50 int num = n;
51 for(int i=1;i<n;i++)
52 for(int j=i+1;j<=n;j++)
53 if(inter(l[i],l[j])){
54 isv[i] = 1;
55 num--;
56 break;
57 }
58
59 printf("Top sticks: ");
60 //输出所有存在的线段(即最顶上的线段)
61 for(int i=1;i<=n;i++)
62 if(!isv[i]){ //是顶
63 num--;
64 if(num==0)
65 printf("%d.\n",i);
66 else
67 printf("%d, ",i);
68 }
69 }
70 return 0;
71 }
Freecode : www.cnblogs.com/yym2013