L2-028 秀恩爱分得快

题目 L2-028 秀恩爱分得快

image-94b27a6f

思路分析

pat的模拟题输出太恶心了

过不了全部 不管了 题解

image-a04285f5 image-5b2b519a

代码实现

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int ,int>;
using Pll = pair<ll,ll>;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;cin>>n>>m;
	unordered_map<int, unordered_map<int, double>> rela;
	while(m--){
		int num;cin>>num;
		vector<int> people(num);
		for(int i=0;i<num;i++){
			cin>>people[i];
		}
		for (int i=0;i<num;++i){
			for (int j=i+1;j<num;++j) {
				rela[people[i]][people[j]] += 1.0 / num;
				rela[people[j]][people[i]] += 1.0 / num;
			}
		}
	}

	int A, B;
	cin >> A >> B;

	double maxA = 0;
	vector<int> listA;
	for (auto &p : rela[A]) {
		if ((A > 0 && p.first < 0) || (A < 0 && p.first > 0)) {
			if (p.second > maxA) {
				maxA = p.second;
				listA.clear();
				listA.push_back(p.first);
			} else if (p.second == maxA) {
				listA.push_back(p.first);
			}
		}
	}

	double maxB = 0;
	vector<int> listB;
	for (auto &p : rela[B]) {
		if ((B > 0 && p.first < 0) || (B < 0 && p.first > 0)) {
			if (p.second > maxB) {
				maxB = p.second;
				listB.clear();
				listB.push_back(p.first);
			} else if (p.second == maxB) {
				listB.push_back(p.first);
			}
		}
	}

	bool isBestCouple = (maxA == rela[A][B]) && (maxB == rela[B][A]);

	if (isBestCouple) {
		cout << A << " " << B << endl;
	} else {
		sort(listA.begin(), listA.end(), [](int x, int y) {
			return abs(x) < abs(y);
		});
		sort(listB.begin(), listB.end(), [](int x, int y) {
			return abs(x) < abs(y);
		});
		for (int v : listA) cout << A << " " << v << endl;
		for (int v : listB) cout << B << " " << v << endl;
	}

	return 0;
}

同类题型

视频讲解


⬅️ L2-027 名人堂与代金券 🏠 00-天梯赛 ➡️ L2-029 特立独行的幸福