A - AtCoder Group Contest
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
There are 3N participants in AtCoder Group Contest. The strength of the i-th participant is represented by an integer ai. They will form N teams, each consisting of three participants. No participant may belong to multiple teams.
The strength of a team is defined as the second largest strength among its members. For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.
Find the maximum possible sum of the strengths of N teams.
Constraints
- 1≤N≤105
- 1≤ai≤109
- ai are integers.
Input
Input is given from Standard Input in the following format:
N a1 a2 … a3N
Output
Print the answer.
Sample Input 1
2 5 2 8 5 1 5
Sample Output 1
10
The following is one formation of teams that maximizes the sum of the strengths of teams:
- Team 1: consists of the first, fourth and fifth participants.
- Team 2: consists of the second, third and sixth participants.
Sample Input 2
10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
10000000000
The sum of the strengths can be quite large.
思路:先对数由小到大排序,显然最优解选择的中位数是3n-1,3n-3,3n-5......加起来就行。
# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
int a[300003];
int main()
{
int n;
scanf("%d",&n);
for(int i=0; i<3*n; ++i)
scanf("%d",&a[i]);
sort(a, a+3*n, greater<int>());
long long sum = 0;
for(int i=1, j=0; j<n; i+=2)
sum += a[i], ++j;
printf("%lld\n",sum);
return 0;
}