Ball in Berland

This is a programing problem on Codeforces with a difficulty score of 1400.
Its solution is based on the Inclusion-Exclusion principle.

https://codeforces.com/problemset/problem/1475/C

void solve(){
	int a, b, k;
	cin >> a >> b >> k;

	map<pair<int, int>, int> mapp;
	map<int, int> boys, girls;

	vector<pair<int, int>> couples(k);
	for (int i = 0; i < k; ++i){
		cin >> couples[i].first;
		boys[couples[i].first] ++;
	}
	for (int i = 0; i < k; ++i){
		cin >> couples[i].second;
		girls[couples[i].second] ++;
		mapp[couples[i]] ++;
	}

	long long ans = 0;
	for (int i = 0; i < k; ++i){
		ans += (k - 1);
		auto[x, y] = couples[i];
		ans -= boys[x] - 1;
		ans -= girls[y] - 1;
		ans += mapp[{x, y}] - 1;
	}

	cout << ans / 2 << '\n';
}
posted @ 2024-02-27 20:40  _Yxc  阅读(10)  评论(0)    收藏  举报