POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段
题解:数据弱,正向纯模拟可过
但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段,这样就错了
因为如果线段8与5,6,7相交了,我们接下来不能直接判断4,我们还要找7,6,5与之前哪些相交
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<iomanip> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1E-8 /*注意可能会有输出-0.000*/ #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型 #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化 #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0 #define mul(a,b) (a<<b) #define dir(a,b) (a>>b) typedef long long ll; typedef unsigned long long ull; const int Inf=1<<28; const ll INF=1ll<<60; const double Pi=acos(-1.0); const int Mod=1e9+7; const int Max=100010; struct point { double x,y; }; struct line { point a,b; }; double xmult(point p1,point p2,point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } int isIntersected(point p1,point p2,point l1,point l2) { return (max(p1.x,p2.x)>=min(l1.x,l2.x)) && (max(p1.y,p2.y)>=min(l1.y,l2.y)) && (max(l1.x,l2.x)>=min(p1.x,p2.x)) && (max(l1.y,l2.y)>=min(p1.y,p2.y)) && (xmult(l1,p2,p1)*xmult(p2,l2,p1)>0) && (xmult(p1,l2,l1)*xmult(l2,p2,l1)>0) ; } line sti[Max]; int ans[Max],vis[Max]; int Solve(int n) { int coun=0; for(int i=1;i<=n;++i) { for(int j=i+1;j<=n;++j) { if(isIntersected(sti[i].a,sti[i].b,sti[j].a,sti[j].b)) { vis[i]=0; break; } } } for(int i=n;i;--i) if(vis[i]) ans[coun++]=i; return coun; } int main() { int n; while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;++i) { scanf("%lf %lf %lf %lf",&sti[i].a.x,&sti[i].a.y,&sti[i].b.x,&sti[i].b.y); vis[i]=1; } int coun=Solve(n); printf("Top sticks: "); for(int i=coun-1;~i;--i) { printf("%d%s",ans[i],i?", ":".\n"); } } return 0; }
分类:
ACM_计算几何
【推荐】国内首个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应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决