题目大意:一个小孩不断往地上扔棍子,共n根,求结束后共有多少根不与去他相交。

解法思路:典型的判断线段相交问题,利用快速排斥+双跨立判断相交,最后输出没相交的。

(图片来源:http://www.2cto.com/kf/201308/237246.html)

#include<iostream>
#include<algorithm>
using namespace std;
const int nMax = ;
const int mMax = ;
struct Point
{
double x, y;
}s[nMax], e[nMax];
double mult(Point sp, Point ep, Point op) //叉积
{
return (sp.x-op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y);
}
bool isInter(Point s1, Point e1, Point s2, Point e2)
{
if( min(s1.x, e1.x) <= max(s2.x, e2.x) &&
min(s1.y, e1.y) <= max(s2.y, e2.y) &&
min(s2.x, e2.x) <= max(s1.x, e1.x) &&
min(s2.y, e2.y) <= max(s1.y, e1.y) && //快速排斥
mult(s2, e2, s1) * mult(s2, e2, e1) <= && //双跨立
mult(s1, e1, s2) * mult(s1, e1, e2) <= )
return true;
return false;
}
int main()
{
int n , i ;
while(scanf("%d", &n) == && n != ) //每当输入一组数据成功
{
int ans[mMax] , m = ;
for(i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &s[i].x, &s[i].y, &e[i].x, &e[i].y);
int pos = -;
for(int j = ; j < m; j ++)
{
if(ans[j] == -)
pos = j;
else
if(isInter(s[i], e[i], s[ans[j]], e[ans[j]]))
ans[j] = -;
}
if(pos == -)
ans[m++] = i;
else
ans[pos] = i;
}
i = ;
sort(ans, ans + m); //升序排列,按照输入顺序
while(ans[i] == -)
i++;
printf("Top sticks:");
while(i < m-)
printf(" %d,", ans[i ++]);
printf(" %d.\n", ans[i]);
}
return ;
}

  !!!!这个代码有点问题,但给的两组都过了,求大神指点.....

//这个代码有点问题,但给的两组都过了,求大神指点.....
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
double x,y;
}s,e;
struct side
{
node s,e;
int nth;
}temp;
inline int multy(const node &p0,const node &p1,const node &p2)    //叉积
{
return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}
inline bool is_insert(const node &s1,const node &e1,const node &s2,const node &e2)  //判交
{
if( min(s1.x, e1.x) <= max(s2.x, e2.x) &&
min(s1.y, e1.y) <= max(s2.y, e2.y) &&
min(s2.x, e2.x) <= max(s1.x, e1.x) &&
min(s2.y, e2.y) <= max(s1.y, e1.y) && //快速排斥
multy(s2, e2, s1) * multy(s2, e2, e1) <= && //双跨立
multy(s1, e1, s2) * multy(s1, e1, e2) <= )
return true;
return false;
}
inline bool cmp(const side &a,const side &b)
{
return (a.nth<b.nth);
}
int main()
{
int n;
while(scanf("%d",&n)==&&n)
{
vector<side>v;
for(int i=;i<n;i++)
{
scanf("%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y);
bool f=true,first=true;
for(int j=;j<v.size();j++)
if(is_insert(s,e,v[j].s,v[j].e))
if(first)
v[j].s=s,v[j].e=e,v[j].nth=i,f=false,first=false;    //第一次相交替换
else
v.erase(v.begin()+j);        //第二次相交直接删
if(f)
{
temp.s=s,temp.e=e,temp.nth=i;
v.push_back(temp);
}
}
sort(v.begin(),v.end(),cmp);      //按顺序输出
printf("Top sticks:");
for(int i=;i<v.size()-;i++)
printf(" %d,",v[i].nth+);
v[v.size()-].nth++;
printf(" %d.\n",v[v.size()-].nth);    //注意格式
}
return ;
}

POJ 2653的更多相关文章

  1. 线段相交 POJ 2653

    // 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...

  2. poj 2653 线段与线段相交

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 D ...

  3. poj 2653 (线段相交判断)

    http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submis ...

  4. POJ 2653 - Pick-up sticks - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...

  5. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  6. POJ 2653 Pick-up sticks【线段相交】

    题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...

  7. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  8. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  9. 计算几何--判断两条线段相交--poj 2653

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8862   Accepted: 3262 De ...

随机推荐

  1. UVA 11624 Fire!

    题目大意: F代表火焰 J代表人 一个人想在火焰烧到自己之前逃出着火地区 . 为路,人可以走,火可以燃烧(当然如果火先烧到就不能走了) #为墙,不可走 如果能逃出,输出时间,不能,输出IMPOSSIB ...

  2. blade and soul Personal Combos

    Personal Combos Since Blade and Soul is mainly based on skills, the game is more interesting after y ...

  3. 【DIY】【外壳】木板 & 亚克力 加工

    —————————————————————————————————————————————————————————————————————— 一.途径 淘宝 https://item.taobao.c ...

  4. iOS UISearchBar 设置取消按钮,回收键盘,并修改cancel为“取消”

    继承协议: UISearchBarDelegate 在代理方法中设置: #pragma mark --- 搜索框开始编辑 --- - (void)searchBarTextDidBeginEditin ...

  5. Android中的自定义视图控件

    简介 当现有控件不能满足需求时,就需要自定义控件. 自定义控件属性 自定义控件首先要继承自View,重写两个构造函数. 第一个是代码中使用的: public MyRect(Context contex ...

  6. TRACERT命令

    这半个月  服务器从联通线路换到移动线路 导致基层用联通和电信线路的用户 上不去收费软件 tracert 120.194.42.142:8090 发现路由器 解析地址绕过很多条街后 出现丢包现象 联系 ...

  7. Android按钮的各个样式设置

    安卓开发学习之014 Button应用详解(样式.背景.按钮单击.长按.双击.多击事件) 一.Button简介 按钮也是继承自TextView 二.XML定义方法 <Button android ...

  8. Calculator(1.5)

    Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...

  9. Adding a WebPart to a SharePoint 2013 Master Page 分类: Sharepoint 2015-07-08 01:03 7人阅读 评论(0) 收藏

    On SharePoint 2013 you can not add the Web Parts to the master page the same way of 2010. Please use ...

  10. Ftrl in tensorflow

    reference :点击这里https://github.com/tensorflow/tensorflow/issues/3725 讲解 http://www.tuicool.com/articl ...