CodeForces - 160A Twins排序

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 160A Twins排序相关的知识,希望对你有一定的参考价值。

A. Twins
time limit per test2 seconds
memory limit per test256 megabytes

Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It’s hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it’s like.

Now let’s imagine a typical morning in your family. You haven’t woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, …, an. But as Mom was running out of time, she didn’t split the coins for you two. So she scribbled a note asking you to split the money equally.

As you woke up, you found Mom’s coins and read her note. “But why split the money equally?” — you thought. After all, your twin is sleeping and he won’t know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you’ve decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.

Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, …, an (1 ≤ ai ≤ 100) — the coins’ values. All numbers are separated with spaces.

Output
In the single line print the single number — the minimum needed number of coins.

Examples
input
2
3 3
output
2
input
3
2 1 2
output
2
Note
In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins’ sum.

In the second sample one coin isn’t enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.

问题链接CodeForces - 160A Twins
问题简述:(略)
问题分析:(略)

AC的C++语言程序如下:

/* CodeForces - 160A Twins */

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 100;
int a[N];

int main()

    int n, sum = 0, ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) 
        scanf("%d", &a[i]);
        sum += a[i];
    

    sort(a, a + n);

    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) 
        ans += a[i];
        sum -= a[i];
        cnt++;
        if (ans > sum) 
            printf("%d\\n", cnt);
            break;
        
    

    return 0;

以上是关于CodeForces - 160A Twins排序的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 160A-C语言解题报告

monozygotic (MZ) twins 和 dizygotic (DZ) twins

PSTAT 160A PYTHON HOMEWORK

NeurIPS 2021 | Twins:重新思考高效的视觉注意力模型设计

C - Twins

A . Find the Twins -UCF Local Programming Contest 2015