嘟嘟嘟




本来我要写feng shui这道题的。然后网上都说什么半平面相交,于是我还得现学这个东西,就来刷这道模板题了。




所谓的半平面相交和高中数学的分数规划特别像。比如这道题,把每一条边看成一条有向直线,则合法的范围都是直线的右半部分,最后求交集。大概是每一次都取一半,所以就叫半平面相交吧。




\(O(n ^ 2)\)的做法很简单,我也只会\(O(n ^ 2)\)的。枚举每一条边,然后用这条边去切当前算出来的图形。

具体怎么切?一句话就是把这条直线左边的点全部扔掉。

放个伪代码就明白了:

for 每条边ai ai+1
if (ai在AB右边)
把ai加入答案
if (ai+1在AB左边) 把交点加入答案
else if(ai+1在AB右边) 把交点加入答案

至于判断左右,用叉积求又向面积就行了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m, cnt = 0;
struct Point
{
db x, y;
Point operator - (const Point& oth)const
{
return (Point){x - oth.x, y - oth.y};
}
db operator * (const Point& oth)const
{
return x * oth.y - oth.x * y;
}
Point operator * (const db& d)const
{
return (Point){x * d, y * d};
}
}p[maxn], a[maxn]; int tot = 0;
Point b[maxn];
db cross(Point A, Point B, Point C)
{
return (B - A) * (C - A);
}
void addCross(Point A, Point B, Point C, Point D)
{
db s1 = (C - A) * (D - A), s2 = (D - B) * (C - B);
b[++tot] = A - (A - B) * (s1 / (s1 + s2));
}
void cut(Point A, Point B)
{
tot = 0;
a[cnt + 1] = a[1];
for(int i = 1; i <= cnt; ++i)
{
if(cross(A, B, a[i]) >= 0)
{
b[++tot] = a[i];
if(cross(A, B, a[i + 1]) < 0) addCross(A, B, a[i], a[i + 1]);
}
else if(cross(A, B, a[i + 1]) > 0) addCross(A, B, a[i], a[i + 1]);
}
for(int i = 1; i <= tot; ++i) a[i] = b[i];
cnt = tot;
} int main()
{
n = read(); m = read();
for(int i = 1; i <= m; ++i) a[i].x = read(), a[i].y = read();
cnt = m; n--;
while(n--)
{
m = read();
for(int i = 1; i <= m; ++i) p[i].x = read(), p[i].y = read();
p[m + 1] = p[1];
for(int i = 1; i <= m; ++i) cut(p[i], p[i + 1]);
}
a[cnt + 1] = a[1];
db ans = 0;
for(int i = 1; i <= cnt; ++i) ans += a[i] * a[i + 1];
printf("%.3lf\n", ans / 2);
return 0;
}

[CQOI2006]凸多边形(半平面相交)的更多相关文章

  1. bzoj 2618: [Cqoi2006]凸多边形 [半平面交]

    2618: [Cqoi2006]凸多边形 半平面交 注意一开始多边形边界不要太大... #include <iostream> #include <cstdio> #inclu ...

  2. 洛谷 P4196 [CQOI2006]凸多边形 (半平面交)

    题目链接:P4196 [CQOI2006]凸多边形 题意 给定 \(n\) 个凸多边形,求它们相交的面积. 思路 半平面交 半平面交的模板题. 代码 #include <bits/stdc++. ...

  3. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  4. bzoj 2618 2618: [Cqoi2006]凸多边形(半平面交)

    2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 656  Solved: 340[Submit][Status] ...

  5. 2018.07.04 BZOJ 2618 Cqoi2006凸多边形(半平面交)

    2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec Memory Limit: 128 MB Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n ...

  6. 【BZOJ2618】[CQOI2006]凸多边形(半平面交)

    [BZOJ2618][CQOI2006]凸多边形(半平面交) 题面 BZOJ 洛谷 题解 这个东西就是要求凸多边形的边所形成的半平面交. 那么就是一个半平面交模板题了. 这里写的是平方的做法. #in ...

  7. [CQOI2006]凸多边形(半平面交)

    很明显是一道半平面交的题. 先说一下半平面交的步骤: 1.用点向法(点+向量)表示直线 2.极角排序,若极角相同,按相对位置排序. 3.去重,极角相同的保留更优的 4.枚举边维护双端队列 5.求答案 ...

  8. 【半平面交】bzoj2618 [Cqoi2006]凸多边形

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...

  9. bzoj2618: [Cqoi2006]凸多边形

    Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一行有一个整数n,表示凸多边形的个数,以下依 ...

随机推荐

  1. C# 泛型使用笔记

    泛型的基本概念我就不在这重复了,不了解的同学请自行百度. 我主要写下我在项目中要到的泛型实例.献丑了.....有什么不好或不对的地方大家尽可评论留言. 为什么要用泛型? 通过使用泛型,我们可以极大地提 ...

  2. Asp.Net 之字符串和集合的使用

    一:object:所有类的基类,所有类都直接或者间接继承自object 二:string 字符串的定义:string str=””    string str=new string(new char[ ...

  3. 二、hdfs单节点安装

    一.准备环境 在配置hdfs之前,我们需要先安装好hadoop的配置,本文主要讲述hdfs单节点的安装配置. hadoop的单节点安装配置请参考:https://www.cnblogs.com/lay ...

  4. JAVA获取5位随机数

    package baofoo.utils; import org.junit.Test; import java.text.SimpleDateFormat; import java.util.Dat ...

  5. No Mapping For GET "xxx.do"

    今天写的一个form表单提交时总是报错找不到mapping,form如下: <form action="toUpdate.do" method="post" ...

  6. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  7. 通用CSS命名规范

    一.文件命名规范 样式文件命名主要的 master.css布局,版面 layout.css专栏 columns.css文字 font.css打印样式 print.css主题 themes.css [/ ...

  8. 事件处理程序DOM0,DOM2,IE的区别总结

    一.事件流   顺序 备注 事件冒泡 目标对象~document对象   事件捕获 document对象~目标对象 老版本浏览器不支持 DOM事件流 document对象~目标对象~document对 ...

  9. Java中的数据类型转换

    先来看一个题: Java类Demo中存在方法func0.func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{ float fu ...

  10. SSM 框架-02-MyEclipse 2017 安装与破解

    SSM 框架-02-MyEclipse 2017 安装与破解 现在在学J2EE,然后使用的工具就是 MyEclipse,现在就抛弃 Eclipse 了,我就不多说它俩的区别了,但是 MyEclipse ...