POJ 2653 Pick-up sticks

计算几何,判断线段相交

注意题目中的一句话:You may assume that there are no more than 1000 top sticks.

我认为是没有描述清楚的,如果不是每次扔完都保证不超过1000,此题很难做吧。

如果每次扔完保证小于1000根在顶部,那么暴力即可。

开一个vector保存每次扔完在顶部的棒子,下一次扔的时候,只要在删除vector中与扔的相交的棒子,然后再把这个棒子压入进来,最后留下的就是答案

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
const double eps=1e-8;
int totP,totL;
struct point
{
    double x;
    double y;
} p[2*maxn];
struct Line
{
    point a;
    point b;
    int id;
} line[maxn];
vector<Line> v;
vector <Line>::iterator Iter;
vector<int> ans;

int flag[maxn];

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 opposite_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)<-eps;
}

int intersect_ex(point u1,point u2,point v1,point v2)
{
    return opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);
}

int main()
{
    while(~scanf("%d",&totL))
    {
        if(!totL) break;
        totP=0;
        for(int i=0; i<totL; i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            p[totP+0].x=a;
            p[totP+0].y=b;
            p[totP+1].x=c;
            p[totP+1].y=d;

            line[i].a=p[totP+0];
            line[i].b=p[totP+1];
            line[i].id=i;
            totP=totP+2;
        }
        v.clear();
        for(int i=0; i<totL; i++)
        {
            for (Iter=v.begin();Iter!=v.end();)
            {
                Line now=*Iter;
                if(intersect_ex(line[i].a,line[i].b,now.a,now.b))
                    Iter = v.erase(Iter);
                else Iter++;
            }
            v.push_back(line[i]);
        }

        ans.clear();
        for (Iter=v.begin();Iter!=v.end();Iter++ )
        {
            Line now=*Iter;
            ans.push_back(now.id);
        }
        printf("Top sticks: ");
        for(int i=0; i<ans.size(); i++)
        {
            printf("%d",ans[i]+1);
            if(i<ans.size()-1) printf(", ");
            else printf(".\n");
        }
    }
    return 0;
}
时间: 2024-12-26 13:21:45

POJ 2653 Pick-up sticks的相关文章

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

一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 100005 using namespace std; struct Point { double x

poj 2653 线段与线段相交

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 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 fin

poj 2653 (线段相交判断)

http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9531   Accepted: 3517 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishi

POJ 2653

开一个数组做成队列来搜索就好了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 8 const int MAXN=100010; 9 10 typedef struct po{ 11 double x,y; 12 }Point; 13

poj 2653 计算几何

1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstdio> 6 using namespace std; 7 struct point { 8 double x,y; 9 }; 10 point be[100005],en[100005]; 11 int ans[100005]; 12 int re

POJ 2653 线段交

思路: 运用队列存储没有被覆盖的木棍,没加入一个棍子,就要判断一下是否队列中的棍子被覆盖,如果被覆盖,就从队列中删除: 线段交判断方法:跨立实验 Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9698 Accepted: 3591 Description Stan has n sticks of various length. He throws them one at a time on th

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2015南阳CCPC D - Pick The Sticks dp

D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao

【POJ】2513 Colored Sticks

字典树+并查集. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 500005 6 #define MAXL 11 7 #define TRIEN 26 8 9 typedef struct Trie { 10 int v; 11 Trie *next[TRIEN]; 12 Trie() { 13 v = 0; 14 for (int i=0; i<TRI