C1C2. Power Transmission (gcd离散化边)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38772011/article/details/90109733

https://codeforces.com/contest/1163/problem/C1

题意:给出二维平面上的至多1000个点,求两两连边后,相互相交的边的对数。

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
#define x first
#define y second

const int N = 1005;

int n, x[N], y[N];
set<tuple<int, int, int>> ss;
map<tuple<int, int>, ll> mp;

int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d%d", x + i, y + i);
		for(int j = 0; j < i; j++) {
			pii p = pii(x[i], y[i]);
			pii q = pii(x[j], y[j]);
			if(p > q) swap(p, q);
			int a = q.x - p.x;
			int b = q.y - p.y;
			int g = __gcd(a, b);
			a /= g;
			b /= g;
			int c = b * p.x - a * p.y;
			ss.insert(tie(a, b, c));
		}
	}
	ll r = ss.size();
	r = r * (r - 1) / 2;
	for(auto i : ss) {	
		int x, y;
		tie(x, y, ignore) = i;
		mp[tie(x, y)]++;
	}
	for(auto i : mp) r -= i.y * (i.y - 1) / 2;
	printf("%lld\n", r);
}

猜你喜欢

转载自blog.csdn.net/weixin_38772011/article/details/90109733
今日推荐