POJ2653 Pick-up sticks(线段相交,暴力?)
程序员文章站
2022-06-03 19:43:55
...
Pick-up sticks
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14261 | Accepted: 5398 |
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.
题意:按先后顺序放木棒,问最后没有被压着的木棒有哪些?
思路:线段相交问题,不过本题暴力就能过,可能是数据太水,没想出nlogn做法.
参考文章:判断线段是否相交
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
struct node
{
double x,y;
};
struct line
{
node a,b;
} l[maxn];
int n,vis[maxn],ans[maxn];
bool judge(line p,line q)//判断线段是否相交
{
//快速排斥实验
if(max(p.a.x,p.b.x)< min(q.a.x,q.b.x))
return 0;
if(max(q.a.x,q.b.x)< min(p.a.x,p.b.x))
return 0;
if(max(p.a.y,p.b.y)< min(q.a.y,q.b.y))
return 0;
if(max(q.a.y,q.b.y)< min(p.a.y,p.b.y))
return 0;
//跨立实验
double u,v,w,z;
node a = p.a,b = p.b,c = q.a,d = q.b;
u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
return (u*v<= esp&& w*z<= esp);
}
void init()
{
mem(vis,0);
mem(ans,0);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init();
for(int i = 1;i<= n;i++)
scanf("%lf %lf %lf %lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
int cnt = 0;
for(int i = 1;i<= n;i++)
{
int j;
for(j = i+1;j<= n;j++)
{
if(judge(l[i],l[j]))
break;
}
if(j == n+1)
ans[++cnt] = i;
}
printf("Top sticks:");
for(int i = 1;i< cnt;i++)
printf(" %d,",ans[i]);
printf(" %d.\n",ans[cnt]);
}
return 0;
}
下一篇: 傻傻分不清楚的php脚本途径
推荐阅读
-
计算几何——Pick-up sticks(线段相交)
-
POJ2653 Pick-up sticks(线段相交,暴力?)
-
POJ 2653 Pick-up sticks (线段与线段相交)
-
POJ 2653--Pick-up sticks(判断线段相交)
-
POJ 2653 Pick-up sticks(线段相交)
-
poj2653 Pick-up sticks(判断线段相交)
-
POJ2653 Pick-up sticks —— 两线段相交(模板题)
-
Pick-up sticks POJ2653(线段相交判断)
-
[POJ2653]Pick-up sticks(计算几何-叉积)
-
POJ2653 Pick-up sticks