AtCoder Grand Contest 001 A - BBQ Easy 简单排序

Problem Statement

Snuke is having a barbeque party.

At the party, he will make N servings of Skewer Meal.

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 iLi 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);
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting) 是一道比较有趣的题目。它是一道交互式的排序题目,需要你与一个神秘程序进行交互,以便将一串无序的数字序列排序。 具体来说,这个神秘程序会给你一个长度为 $N$ 的数字序列,然后你需要通过询问它两个数字的大小关系,来逐步确定这个序列的排序顺序。每次询问之后,神秘程序都会告诉你两个数字的大小关系,比如第一个数字比第二个数字小,或者第二个数字比第一个数字小。你需要根据这个信息,来调整这个数字序列的顺序,然后再向神秘程序询问下一对数字的大小关系,以此类推,直到这个数字序列被完全排序为止。 在这个过程中,你需要注意以下几点: 1. 你最多只能向神秘程序询问 $Q$ 次。如果超过了这个次数,那么你的程序会被判定为错误。 2. 在每次询问之后,你需要及时更新数字序列的顺序。具体来说,如果神秘程序告诉你第 $i$ 个数字比第 $j$ 个数字小,那么你需要将这两个数字交换位置,以确保数字序列的顺序是正确的。如果你没有及时更新数字序列的顺序,那么你的程序也会被判定为错误。 3. 在询问的过程中,你需要注意避免重复询问。具体来说,如果你已经询问过第 $i$ 个数字和第 $j$ 个数字的大小关系了,那么你就不需要再次询问第 $j$ 个数字和第 $i$ 个数字的大小关系,因为它们的大小关系已经被确定了。 4. 在排序完成之后,你需要将排序结果按照从小到大的顺序输出。如果你输出的结果不正确,那么你的程序也会被判定为错误。 总的来说,这道题目需要你熟练掌握交互式程序设计的技巧,以及排序算法的实现方法。如果你能够熟练掌握这些技巧,那么就可以顺利地完成这道非传统题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值