BZOJ 2564 集合的面积 几何 闵科夫斯基求和

给出两个点集,然后求问来自己两个不同点集的点 ( x 1 , y 1 ) (x_{1},y_{1}) (x1,y1) ( x 2 , y 2 ) (x_{2},y_{2}) (x2,y2)构成的 ( x 1 + x 2 , y 1 + y 2 ) (x_{1}+x_{2},y_{1}+y_{2}) (x1+x2,y1+y2)所组成的凸包的面积的两倍。
先对两个点集分别求凸包,然后双指针在凸包上扫描,每次假如更凸的那个边。
先存一个板子,虽然好像目前还存在不少问题。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=LONG_LONG_MAX;
const int N=2e5+7;
struct Point {
	ll x,y;
	Point() {}
	Point(int _x,int _y):x(_x),y(_y) {}
	bool operator <(const Point &rhs) const {
		if(x<rhs.x) return 1;
		else if(x==rhs.x&&y<rhs.y) return 1;
		else return 0;
	}
	Point operator +(Point B) {
		return Point(x+B.x,y+B.y);
	} 
	Point operator -(Point B) {
		return Point(x-B.x,y-B.y);
	} 
	Point operator *(int k) {
		return Point(k*x,k*y);
	}
	ll dot(Point B) {
		return x*B.x+y*B.y;
	}
	ll det(Point B) {
		return x*B.y-y*B.x;
	}
}a[N],b[N],ch1[N],ch2[N],ch3[N],ch4[N];
int ConvexHull(Point a[],int n,Point ch[]) {
	sort(a+1,a+1+n);
	int tot=0;
	for(int i=1;i<=n;i++) {
		while(tot>1&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<0) tot--;
		ch[++tot]=a[i];
	}
	int ctrl=tot;
	for(int i=n-1;i>=1;i--) {
		while(tot>ctrl&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<0) tot--;
		ch[++tot]=a[i];
	}
	return tot;
}
int ConvexHull2(Point a[],int n,Point ch[]) {
	sort(a+1,a+1+n);
	int tot=0;
	for(int i=1;i<=n;i++) {
		while(tot>1&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<=0) tot--;
		ch[++tot]=a[i];
	}
	int ctrl=tot;
	for(int i=n-1;i>=1;i--) {
		while(tot>ctrl&&(ch[tot]-ch[tot-1]).det(a[i]-ch[tot])<=0) tot--;
		ch[++tot]=a[i];
	}
	return tot-1;
}
int Merge(Point ch1[],Point ch2[],Point ch[],int n,int m) {
	int tot=0;
	ch[++tot]=ch1[1]+ch2[1];
	int i=1,j=1;
	while(i+1<=n&&j+1<=m) {
		Point n1=(ch1[i]+ch2[j+1])-ch[tot];
		Point n2=(ch1[i+1]+ch2[j])-ch[tot];
		if(n1.det(n2)<0) {
			ch[++tot]=ch1[i+1]+ch2[j];i++;
		}
		else {
			ch[++tot]=ch1[i]+ch2[j+1];j++; 
		} 
	}
	for(;i<=n;i++) ch[++tot]=ch1[i]+ch2[m];
	for(;j<=m;j++) ch[++tot]=ch2[j]+ch1[n];
	return tot;
}
int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) 
		scanf("%lld%lld",&a[i].x,&a[i].y);
	for(int i=1;i<=m;i++) 
		scanf("%lld%lld",&b[i].x,&b[i].y);
	ll ans=0;
	int sz1=ConvexHull(a,n,ch1);
	int sz2=ConvexHull(b,m,ch2);
	int tot=Merge(ch1,ch2,ch3,sz1,sz2);
	int sz3=ConvexHull2(ch3,tot,ch4);
	for(int i=2;i<sz3;i++) 
		ans+=(ch4[i]-ch4[1]).det(ch4[i+1]-ch4[1]); 
	printf("%lld\n",ans); 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值