PTA show affection quickly (two-dimensional vector)

L2-028 Show your affection quickly (25 points)

analysis:

There are 10,000 holes in this question:

Pit 1: The serial number 0 may be female, that is, -0. At this time, there is no difference in reading the plastic shape. You need to use getchar() to read, and it is more convenient to use a function to read.

Pit 2: The person storing the photos must separate men and women in order to meet the time requirements, and only calculate the intimacy between the opposite sex.

Pit 3: When judging whether each other is the closest person, consider that the maximum intimacy value of the two parties is the same, but not the situation of the other party.

Code:

#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;
}

 

Guess you like

Origin blog.csdn.net/qq_43700916/article/details/88827106
Recommended