[Algorithm] Group competition, the maximum comprehensive strength of the group

topic:

Enter n, which means there are n groups, each group has 3 people

Everyone has a number corresponding to their strength, enter the strength of each person, and the strength of each group is the median of the strength of its team members

Output sum, sum is the sum of all group strengths after grouping, and sum is the maximum value

algorithm:

Greedy algorithm, first sort all the team members according to their strength, form a group with the strongest strength and the weakest strength, and then divide the strongest strength among the remaining team members into this group as the median, so that The sum of the comprehensive strength of each group is the largest

source code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<int> v(3 * n);
	for (int i = 0; i < 3 * n; ++i)
	{
		cin >> v[i];
	}
	
	sort(v.begin(), v.end());
	int sum = 0;
	int cur = v.size() - 2;
	for (int i = 0; i < n; ++i)
	{
		sum += v[cur];
		cur -= 2;
	}
	cout << sum << endl;
	
	return 0;
}

Guess you like

Origin blog.csdn.net/phoenixFlyzzz/article/details/130441226