PTA 秀恩爱分得快 (二维vector)

L2-028 秀恩爱分得快 (25 分)

分析:

本题有一万个坑:

坑一:序号0用可能为女,即-0,这时整形读取是没有区别的,要用getchar()来读,用一个函数来读取较为方便。

坑二:存储照片上的人要把男女分开,才可以满足时间要求,就只计算异性性间的亲密度。

坑三:在判断是否彼此为最亲近的人时,考虑两方最大亲密值相同,但不是对方的情况。

代码:

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

double friends[1000][1000];
bool sex[1000];//0--man   1--woman

int Read()
{
	bool flag = 0;
	char a = getchar();
	int ans = 0;
	while ((a<'0' || a>'9') && a != '-')
	{
		a = getchar();
	}
	while (a == '-' || a >= '0'&&a <= '9')
	{
		if (a == '-')flag = 1;
		else
		{
			ans = ans * 10 + a - '0';
		}
		a = getchar();
	}
	sex[ans] = flag;
	return ans;
}

void Print(int a, int b)
{
	if (sex[a])cout << '-';
	cout << a << ' ';
	if (sex[b])cout << '-';
	cout << b << endl;
}

int main()
{
	int popu, picture;
	cin >> popu >> picture;
	while (picture--)
	{
		vector <int> gender[2];//0--man   1--woman
		int poto_popu;
		cin >> poto_popu;
		double cnt = poto_popu;
		while (poto_popu--)
		{
			int number = Read();
			gender[sex[number]].push_back(number);
		}
		for (int i = 0; i<gender[0].size(); i++)
		{
			for (int j = 0; j<gender[1].size(); j++)
			{
				friends[gender[0][i]][gender[1][j]] += 1 / cnt;
				friends[gender[1][j]][gender[0][i]] += 1 / cnt;
			}
		}
	}

	int cp1 = Read(), cp2 = Read();
	double max1 = 0, max2 = 0;
	for (int i = 0; i<popu; i++)
	{
		max1 = max(max1, friends[cp1][i]);
		max2 = max(max2, friends[cp2][i]);
	}

	if (max1 == friends[cp1][cp2] && max2 == friends[cp1][cp2])
	{
		Print(cp1, cp2);
	}
	else
	{
		for (int i = 0; i<popu; i++)
		{
			if (friends[i][cp1] == max1)Print(cp1, i);
		}
		for (int i = 0; i<popu; i++)
		{
			if (friends[i][cp2] == max2)Print(cp2, i);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43700916/article/details/88827106
PTA