HDU 3629 Convex(数论+计数+几何)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3629

题意:给定n个点,求能构成几个凸四边形

解法:详见blog.sina.com.cn/s/blog_64675f540100ksug.html,利用到了组合数学计数的方法,高效算法two pointer,复杂度O(n^2)

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

const double eps = 1e-9;
const double pi = acos(-1.0);
const int N = 705;
int t, n, tn;
struct Point {
	double x, y;
	void read() {
		scanf("%lf%lf", &x, &y);
 	}
} p[N];

double r[2 * N];

__int64 C(int n, int m) {
	if (m > n) return 0;
	__int64 ans = 1;
	for (int i = 0; i < m; i++)
		ans = ans * (n - i) / (i + 1);
	 return ans;
}

double cal(Point a, Point b) {
	return atan2(b.y - a.y, b.x - a.x);
}

__int64 solve(int num) {
	tn = 0;
	__int64 ans = 0;
	for (int i = 0; i < n; i++) {
		if (i == num) continue;
		r[tn++] = cal(p[num], p[i]);
 	}
 	sort(r, r + tn);
	int j = 1;
	for (int i = 0; i < tn; i++)
		r[i + tn] = r[i] + 2 * pi;
  	for (int i = 0; i < tn; i++) {
	  	while (fabs(r[j] - r[i]) - pi < -eps) j++;
  		ans += C(j - i - 1, 2);
	}
	return C(tn, 3) - ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		__int64 ans = 0;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			p[i].read();
		for (int i = 0; i < n; i++) {
			ans += solve(i);
  		}
  		printf("%I64d\n", C(n, 4) - ans);
 	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值