题目大意:一个小孩不断往地上扔棍子,共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. C# 中的 Static

    今天测试了一下C#中 static 的初始化顺序: 1.调用时才初始化, 2.按照调用顺序初始化 3.先执行类的静态方法,然后初始化静态变量及方法 4.继承时,先执行子类的静态方法,然后执行父类的静态 ...

  2. 小游戏Talk表

    [Config]1|0|2|久远的记忆影子|你也是误入宠物王国的妹子吧,我在这里等你很久了,或许我们应该一起逃出这里,跟着我.[Config] [Config]2|3|2|久远的记忆影子|这里原本是一 ...

  3. (DFS、bitset)AOJ-0525 Osenbei

    题目地址 简要题意: 给出n行m列的0.1矩阵,每次操作可以将任意一行或一列反转,即这一行或一列中0变为1,1变为0.问通过任意多次这样的变换,最多可以使矩阵中有多少个1. 思路分析: 行数比较小,先 ...

  4. Eclipse里面Outline中图标的含义

    先说颜色:  绿色:public  黄色:protected  蓝色:no modifier  红色:private 再说形状:  实心:method  空心:variable  实心中间有字母C:c ...

  5. LightOJ 1104

    题意: 给你一年有n天,求至少有m人使得至少有两个人在同一天生日的概率不少于0.5. 分析: 任意两个人不在同一天生日的概率为C(n,m)*m!/n^m,它的对立事件A为至少有两个人在同一天生日, 则 ...

  6. HOJ 1004: Prime Palindromes

    问题:输入两个整数 a 和 b (5 <= a < b <= 1,000,000,000),输出 [a, b] 内的所有回文质数. 最简单的暴力解法是依次遍历 [a, b] 范围内的 ...

  7. C语言中内存对齐

    今天一考研同学问我一个问题,一个结构体有一个int类型成员和一个char类型成员,问我这个结构体类型占多少个字节,我直接编个程序给他看结果.这个结构体占八个字节,咦,当时我蛮纳闷的,一个int类型四个 ...

  8. setProgressBarIndeterminateVisibility(true);

    此为在标题栏 上 设置一个loading 圈  实用...

  9. Oracle存储过程例子:运用了正则表达式、数组等

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...

  10. Visual Studio 不生成.vshost.exe和.pdb文件的方法【转】

    Visual Studio 不生成.vshost.exe和.pdb文件的方法[转] 使用Visual Studio编译工程时,默认设置下,即使选择了「Release」时也会生成扩展名为「.vshost ...