Problem Statement
Snuke is having a barbeque party.
At the party, he will make N servings of Skewer Meal.
![](https://i-blog.csdnimg.cn/blog_migrate/0439bf122220743a367766594e7eb56d.png)
Example of a serving of Skewer Meal
He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is Li. Also, he has an infinite supply of ingredients.
To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers. Let the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.
What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?
Constraints
- 1≦N≦100
- 1≦Li≦100
- For each i, Li is an integer.
Input
The input is given from Standard Input in the following format:
N L1 L2 … L2N
Output
Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.
2 1 3 1 2
3
5 100 1 2 3 14 15 58 58 58 29
135
解题思路
emmm,英文挺复杂的,看看数据就大概能猜出来怎么求的了。
将2 * N的数据从小到大进行排序,然后将奇数位置上的数累加。
也就是N份烤串的最小配料累加,最后求出你最后想得到的answer。
C++ AC代码:
#include <iostream>
#include <algorithm>
using namespace std;int main()
{
int n;
int a[205];
while(cin >> n) {
for(int i = 0; i < 2 * n; i ++)
cin >> a[i];
sort(a , a + 2 * n);
int ans = 0;
for(int i = 2 * n - 1; i >= 0 ; i -= 2)
ans += a[i - 1];
cout << ans << endl;
}
}
JAVA AC代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
int ans = 0;
int [] a = new int [205];
int n = cin.nextInt();
for(int i = 0; i < 2 * n; i ++)
a[i] = cin.nextInt();
Arrays.sort(a, 0, 2 * n);
for(int i = 2 * n - 1; i >= 0; i -= 2)
ans += a[i - 1];
System.out.println(ans);
}
}
}