Revenge of Collinearity hdu 5020

Problem Description
In geometry, collinearity is a property of a set of points, specifically, the property of lying on a single line. A set of points with this property is said to be collinear (often misspelled as colinear).
—Wikipedia

Today, Collinearity takes revenge on you. Given a set of N points in two-dimensional coordinate system, you have to find how many set of <Pi, Pj, Pk> from these N points are collinear. Note that <Pi, Pj, Pk> cannot contains same point, and <Pi, Pj, Pk> and <Pi, Pk, Pj> are considered as the same set, i.e. the order in the set doesn’t matter.

Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begins with an integer N, following N lines, each line contains two integers Xi and Yi, describing a point.

[Technical Specification]

  1. 1 <= T <= 33
  2. 3 <= N <= 1 000
  3. -1 000 000 000 <= Xi, Yi <= 1 000 000 000, and no two points are identical.
  4. The ratio of test cases with N > 100 is less than 25%.

Output
For each query, output the number of three points set which are collinear.

Sample Input

2
3
1 1
2 2
3 3
4
0 0
1 0
0 1
1 1

Sample Output

1
0


给定n个点,求问多少组三元组是共线的;
枚举一个端点,再遍历另一个端点,用 map && pair 将斜率存储;
那么如果有 >=2 的斜率值,显然 ans += num*(num-1)/2;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
typedef pair<int, int> pii;


inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

map<pii, int>Map;

struct node {
	int x, y;
}a[maxn];


int main()
{
	//ios::sync_with_stdio(false);
	int T; rdint(T);
	while (T--) {
		ll ans = 0;
		int n;
		rdint(n); Map.clear();
		ms(a);
		for (int i = 1; i <= n; i++) {
			rdint(a[i].x); rdint(a[i].y);
		}
		for (int i = 1; i <= n; i++) {
			Map.clear();
			for (int j = i + 1; j <= n; j++) {
				int dx = a[j].x - a[i].x;
				int dy = a[j].y - a[i].y;
				int Gcd = gcd(dx, dy);
				dx /= Gcd; dy /= Gcd;
				Map[pii(dx, dy)]++;
			}
			for (map<pii, int>::iterator it = Map.begin(); it != Map.end(); it++) {
				if (it->second >=2) {
					ll num = it->second;
					ans += (ll)(num - 1)*num / 2;
				}
			}
		}
		cout << ans << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/82942796
今日推荐