POJ 2653--Pick-up sticks(判断线段相交)
Pick-up sticks
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14568 | Accepted: 5510 |
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.
Hint
Huge input,scanf is recommended.
Source
- 按顺序随机扔木棒,后扔的木棒才可以在之前的木棒的上面,所以用线段相交去和之后的木棒判断相交,如果相交,则在下面,否则就在上面
-
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 using namespace std; 6 const int MAX = 100000; 7 typedef struct point { 8 double x; 9 double y; 10 point() { 11 12 } 13 point(double a, double b) { 14 x = a; 15 y = b; 16 } 17 }point; 18 typedef struct edge { 19 point start; 20 point end; 21 edge() { 22 23 } 24 edge(point a, point b) { 25 start = a; 26 end = b; 27 } 28 }edge; 29 edge line[MAX], tmp; 30 point p; 31 int n; 32 double x, y, x2, y2; 33 inline double max(double a, double b) { return a > b ? a : b; } 34 inline double min(double a, double b) { return a < b ? a : b; } 35 double multi(point p1, point p2, point p0) { 36 return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y); 37 } 38 bool Across(edge v1, edge v2) { 39 if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) && 40 max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) && 41 max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) && 42 max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) && 43 multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v2.start) > 0 && 44 multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v1.start) > 0 45 ) 46 return true; 47 return false; 48 } 49 int main(void) { 50 while (~scanf("%d", &n) && n) { 51 for (int i = 0; i < n; i++) { 52 scanf("%lf%lf%lf%lf", &x, &y, &x2, &y2); 53 line[i] = edge(point(x, y), point(x2, y2)); 54 } 55 printf("Top sticks:"); 56 for (int i = 0; i < n-1; i++) { //最后一根木棒必定在上面 57 tmp = line[i]; 58 int flag = 0; 59 for (int j = i + 1; j < n; j++) { 60 if (Across(tmp, line[j])) //和之后的木块相交,一定在下面 61 { 62 flag = 1; 63 break; 64 } 65 } 66 if (flag != 1) { 67 printf(" %d,", i + 1); 68 } 69 } 70 printf(" %d.\n", n); 71 } 72 return 0; 73 }
(。・∀・)ノ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架