• [CQOI2006]凸多边形(半平面相交)


    嘟嘟嘟


    本来我要写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
    ", ans / 2);
    	return 0;
    }
    
  • 相关阅读:
    MySQL常用维护管理工具 枫
    sql触发器 枫
    MySQL函数大全 枫
    使用Cscope阅读Linux源码 枫
    ASP.NET文件下载,直接向客户端输出文件 枫
    网站设计数据库集群和数据库垂直分割,水平分割,库表散列 枫
    西点成品分类 枫
    asp函数列表 枫
    [笔记]一道C语言面试题:实现 itoa() 函数
    [笔记]Arduino平台使用US100超声波测距模块的电平模式测距实验
  • 原文地址:https://www.cnblogs.com/mrclr/p/10006669.html
Copyright © 2020-2023  润新知