IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 半平面交的交集问题&POJ2451 Uyuw‘s Concert 题解 -> 正文阅读

[数据结构与算法]半平面交的交集问题&POJ2451 Uyuw‘s Concert 题解

前言

初学半平面交,就遇到了这样巨大的坑,也是挺无语…



判断交集为空的情况

poj2451 Uyuw’s Concert

Description

Prince Remmarguts solved the CHESS puzzle successfully. As an award, Uyuw planned to hold a concert in a huge piazza named after its great designer Ihsnayish.

The piazza in UDF - United Delta of Freedom’s downtown was a square of [0, 10000] * [0, 10000]. Some basket chairs had been standing there for years, but in a terrible mess. Look at the following graph.
img
In this case we have three chairs, and the audiences face the direction as what arrows have pointed out. The chairs were old-aged and too heavy to be moved. Princess Remmarguts told the piazza’s current owner Mr. UW, to build a large stage inside it. The stage must be as large as possible, but he should also make sure the audience in every position of every chair would be able to see the stage without turning aside (that means the stage is in the forward direction of their own).

To make it simple, the stage could be set highly enough to make sure even thousands of chairs were in front of you, as long as you were facing the stage, you would be able to see the singer / pianist – Uyuw.

Being a mad idolater, can you tell them the maximal size of the stage?

Input

In the first line, there’s a single non-negative integer N (N <= 20000), denoting the number of basket chairs. Each of the following lines contains four floating numbers x1, y1, x2, y2, which means there’s a basket chair on the line segment of (x1, y1) – (x2, y2), and facing to its LEFT (That a point (x, y) is at the LEFT side of this segment means that (x – x1) * (y – y2) – (x – x2) * (y – y1) >= 0).

Output

Output a single floating number, rounded to 1 digit after the decimal point. This is the maximal area of the stage.

Sample Input

3
10000 10000 0 5000
10000 5000 5000 10000
0 5000 5000 0

Sample Output

54166666.7

Hint

Sample input is the same as the graph above, while the correct solution for it is as below:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ybGSmyWA-1651236206710)(http://poj.org/images/2451_2.jpg)]
I suggest that you use Extended in pascal and long double in C / C++ to avoid precision error. But the standard program only uses double.

显然是半平面交的板子题

这里我们不讲半平面交,我们只讲一些特例

下面有几个比较特殊的情况

1.向量共线

hack1:(存在向量相反且共线,且交集不是给出向量围成的多边形)

input:
3
9 8 9 0
3 3 3 4
3 2 1 7
output:
0.0

hack2:(存在向量相反且共线,且交集为空)

input:
2
1 1 2 2
2 2 1 1
output:
0.0

至少hack了我之前的那几份代码(逃

对于这种给出向量的题目,首先要注意加入构成外边界的4个向量

注意要逆时针加入(题目说了是向量左侧的平面)

我们寻求适用更为广泛的方法

对于方向相同的共线向量(下面说的极角均指atan2(y,x)

  1. 如果极角大于 0 0 0 小于 π \pi π ,就取较左的那个
  2. 如果极角小于 0 0 0 大于 ? π -\pi ?π ,就取较右的那个
  3. 如果极角等于 0 0 0 就取较上的那个
  4. 如果极角等于 π \pi π 就取较下的那个

这么一听好复杂哇,其实只要一个叉积判断下就好了

预处理一下这些烦人的共线向量,就可以放心halfplane()

具体看后面的代码,不急(逃

2.存在零向量

hack:(这种情况会卡极角排序)

input:
1
1 1 1 1
output:
100000000.0

这种情况一般题目会避免出现,但是不排除有的出题人搞了这种

其实这个情况容易判断,只要在读入的时候特判一下就好了

3.其他hack

目前没有发现什么其他hack,欢迎大家来hack我

4.AC代码

注意POJ的老古董编译器,输出 double要用 printf()+ %f(居然不在题目上提醒)

这个出题人不写spj十分恶心人(无意冒犯),反正我搞了好久(6h+) 😓 😓 😓

代码如下

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
// #define double long double
#define INF ((int)0x3f3f3f3f3f3f3f3f)
#define inf ((int)0xc0c0c0c0c0c0c0c0)
#define N (int)(1e5+15)
const double eps=1e-10;
struct vct{double x,y;}p[N];
#define pf(x) ((x)*(x))
vct operator+(vct a,vct b){return {a.x+b.x,a.y+b.y};}
vct operator-(vct a,vct b){return {a.x-b.x,a.y-b.y};}
vct operator*(vct a,double b){return {a.x*b,a.y*b};}
vct operator/(vct a,double b){return {a.x/b,a.y/b};}
double cross(vct a,vct b){return a.x*b.y-a.y*b.x;}
double dot(vct a,vct b){return a.x*b.x+a.y*b.y;}
double len(vct a){return sqrt(pf(a.x)+pf(a.y));}
struct line
{
    vct p,way;
    double k;
    void mkline(vct a,vct b)
    {
        p=a;way=b;
        k=atan2(b.y,b.x);
    }
    void mk(double x1,double y1,double x2,double y2)
    {
        mkline({x1,y1},{x2-x1,y2-y1});
    }
}a[N];
int dcmp(double x)
{
    if(fabs(x)<=eps)return 0;
    return x>eps?1:-1;
}
bool operator<(line a,line b)
{
    int d=dcmp(a.k-b.k);
    if(d==0)return dcmp(cross(b.p-a.p,b.way))>0;
    return d<0;
}
bool onright(vct a,line b){return dcmp(cross(b.way,a-b.p))<0;}
vct intersect(line a,line b)
{
    double x=cross(b.way,a.p-b.p)/cross(a.way,b.way);
    return a.p+a.way*x;
}
int st,en;
line que[N];
bool halfplane(int n)
{
    que[st=en=1]=a[1];
    for(int i=2; i<=n; i++)
    {
        while(st<en&&onright(p[en],a[i]))--en;
        while(st<en&&onright(p[st+1],a[i]))++st;
        que[++en]=a[i];
        if(st<en)p[en]=intersect(que[en-1],que[en]);
    }
    while(st<en&&onright(p[en],que[st]))--en;
    if(en-st<=1)return 0;
    p[st]=intersect(que[st],que[en]);
    return 1;
}
double calc(vct *a,int n)
{
    double res=0;
    for(int i=1; i<n; i++)
        res+=cross(a[i],a[i+1]);
    res+=cross(a[n],a[1]);
    if(fabs(res/=2)<=eps)res=0;
    return res;
}
signed main()
{
    int n,o=0,oo=0;
    scanf("%lld",&n);
    while(n--)
    {
        double x1,x2,y1,y2;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        if(x1==x2&&y1==y2)continue;
        a[++oo].mk(x1,y1,x2,y2);
    }
    a[++oo].mk(0,0,10000,0);
    a[++oo].mk(10000,0,10000,10000);
    a[++oo].mk(10000,10000,0,10000);
    a[++oo].mk(0,10000,0,0);
    sort(a+1,a+1+oo);
    a[0].k=a[1].k-1;
    for(int i=1; i<=oo; i++)
        if(i==1||dcmp(a[i-1].k-a[i].k))
            a[++o]=a[i];
    if(!halfplane(o))puts("0.0");
    else printf("%.1lf\n",calc(p+st-1,en-st+1));
    return 0;
}

总结

半平面交、旋转卡壳,这俩绝对是我目前见过最毒瘤的算法了 😅 😅 😅

啥?你说仙人掌?这不是毒瘤本瘤吗(逃

转载请说明出处

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-04-30 08:55:25  更:2022-04-30 08:58:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 5:54:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码