Codeforces 1025F Disjoint Triangles (计算几何)

题目链接

https://codeforces.com/contest/1025/problem/F

题解

一道挺有意思的计算几何题 qwq
关键在于注意到任何一对不相交的三角形之间,一定有 \(2\) 条内公切线,而一对相交的三角形之间有 \(0\) 条。
于是枚举两个点,求一下这两个点的连线两侧分别有多少点,就可以求出有多少对三角形以它为公切线,总和除以 \(2\) 就是答案。
这个用经典扫描线套路做即可。
时间复杂度 \(O(n^2\log n)\).

代码

#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define iter iterator
#define riter reversed_iterator
#define y1 Lorem_ipsum_dolor
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int mxN = 2000;
struct Point
{
	llong x,y;
	Point() {}
	Point(int _x,int _y):x(_x),y(_y) {}
	int quadrant() {return y>=0?(x>=0?0:1):(x>=0?3:2);}
} a[mxN+3],b[mxN*2+3];
typedef Point Vector;
Point operator +(const Point &x,const Point &y) {return Point(x.x+y.x,x.y+y.y);}
Point operator -(const Point &x,const Point &y) {return Point(x.x-y.x,x.y-y.y);}
Point operator *(const Point &x,const double &y) {return Point(x.x*y,x.y*y);}
llong Dot(Vector x,Vector y) {return x.x*y.x+x.y*y.y;}
llong Cross(Vector x,Vector y) {return x.x*y.y-x.y*y.x;}
bool cmp_ang(Point x,Point y) {return x.quadrant()!=y.quadrant()?x.quadrant()<y.quadrant():Cross(x,y)>0;}

int n;

int main()
{
	n = read();
	for(int i=1; i<=n; i++) a[i].x = read(),a[i].y = read();
	llong ans = 0ll;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++) b[j] = a[j]-a[i]; swap(b[i],b[1]);
		sort(b+2,b+n+1,cmp_ang);
		for(int j=n+1; j<=n+n-1; j++) b[j] = b[j-(n-1)];
		for(int j=2,k=3; j<=n; j++)
		{
			while(k<j||Cross(b[j],b[k+1])>0) {k++;}
			int cnt1 = k-j,cnt2 = n-2-cnt1;
//			printf("j=%d k=%d cnt1=%d cnt2=%d\n",j,k,cnt1,cnt2);
			ans += 1ll*cnt1*(cnt1-1ll)*cnt2*(cnt2-1ll)/4ll;
		}
	}
	ans/=2ll;
	printf("%I64d\n",ans);
	return 0;
}
上一篇:[poj]3280 Cheapest Palindrome 题解


下一篇:JS 判断设备iOS、Android、Web