[Off] cattle team competition

[Programming questions] team contest
this mentioning link: https:? //Www.nowcoder.com/questionTerminal/6736cc3ffd1444a4a0057dee89be789b orderByHotValue = 1 & page = 1 & onlyReference = false

Heat index: 3071 time limit: C / C ++ 1 second, other languages 2 seconds to space constraints: C / C ++ 32M, other languages 64M
algorithm knowledge of video to explain
beef held a programming contest, participate in the competition have the 3n players, each player We have a level value a_i. now these players want to team up to form a total of n number of teams that each team of three people. Cow found that the level is equal to the value of the team players in the team the second highest value.
For example:
the level of the value of a team of three players are 3,3,3 then the value of the team is level 3.
A team of three levels of value players are 3,2,3 then the value of the team is level 3.
A team level values of the three players are 1,5,2. then level 2 is the value of the team
to make the game more Aspect, Taurus team would like to arrange the horizontal sum of the maximum values for all teams.
As shown in the sample:
if beef is divided into the six members of two teams
if scheme:
TEAM1: {1, 2,5}, Team2: {5,5,8}, which was 7.5 time the total level value
and If the scheme:
TEAM1: {2,5,8}, Team2: {1,5,5}, this time is the sum of the level value 10
is not greater than the sum of scheme 10, the output 10.
input description:
input conduct a first positive integer n (1 ≤ n ≤ 10 ^ 5)
a second row comprising 3n integers a_i (1 ≤ a_i ≤ 10 ^ 9), indicative of the level value for each contestant.
output description:
output an integer represents the maximum level of the total value of all teams.
example 1
input
2
528515
Output
10

Topic resolve
this question the meaning of problems, this is a question seeking the optimal solution, three pair is determined by the value of the number of teams entered n the first line, we ensure maximum value for the sum value of all the teams level, we need to make ranks second highest value as large as possible. Therefore, the maximum value into the far right, is the smallest place on the left.
Problem-solving ideas
main idea 1. This title is a greedy algorithm that are selected locally optimal solution can be seen when the current value of each election, so here is greedy ensure that the second value to the maximum value which can take each group can be selected, every time we try to get the maximum, but the maximum value of each group were less than on the right, we should go in the middle of each of the second largest value .
2. Sort, how to make the value of each group were the second largest in the middle, first in ascending order, a total of 3 * n elements, before [0-n-1] th element in the array must be compared to the back , the smallest value. The minimum number of each group as they came in the far left, the remaining number two and has ordered a group of two, check it on the array index, it is two times the number in each group a large sum to the number of optimum level sum value of all teams get the whole question of the maximum. The corresponding index is then removed marked 3n - 2,3n - 4, 3n - 4 ... n + 2, the position of the element n can be accumulated.
Read the text may be too wound on map:
Here Insert Picture Description
code implementation:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    while(cin>>n)  //首先输入队伍的个数,每队有三人
    {
        vector<int> array;//定义数组
        array.resize(3*n);//给这个数组开空间
        for(int i = 0;i < (3*n);i++)//循环输入数据
        {
            cin>>array[i];
        }
        long long  sum = 0;//sum存放每队水平最优解之和,此题的解
        sort(array.begin(),array.end());
        //将数组中元素进行升序,有序后【0-n-1】这n个数作为每队中的最小值,剩余的数两两一组,去第一个做中间值,
        for(int i = n;i <= 3 * n - 2;i+=2)
        {
            sum += array[i];
        }
        cout<<sum<<endl;
    }
}
Published 51 original articles · won praise 229 · views 10000 +

Guess you like

Origin blog.csdn.net/famur/article/details/105199256