zoukankan      html  css  js  c++  java
  • Codeforces Round #111 (Div. 2) 160A Twins(简单贪心)

    A. Twins

    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 a1a2, ..., 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.

    Sample test(s)

    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, 0correspondingly). 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 (c) Copyright 201

    解题报告:这是昨天的比赛,当时代码写出来了,但是提交的时候才发现注册失败,这道题的题意就是有若干个硬币,把这些硬币分成两份,一个人最少需要那几个硬币才能绝对大于另外一个人的钱数,先把硬币从大到小排序,再贪心即可!

    代码如下:

    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int N = 110;
    int a[N];
    int cmp(const void *a, const void *b)
    {
    return *(int *)b - *(int *)a;
    }
    int main()
    {
    int n, i, sum, sum1, ans;
    scanf("%d", &n);
    sum = 0;
    for (i = 0; i < n; ++i)
    {
    scanf("%d", &a[i]);
    sum += a[i];//求出总和
    }
    qsort(a, n, sizeof(a[0]), cmp);
    sum1 = 0;
    for (i = 0; i < n; ++i)
    {
    sum1 += a[i];
    if (sum1 > sum - sum1)//所得的大于剩余的
    {
    ans = i + 1;
    break;
    }
    }
    printf("%d\n", ans);
    return 0;
    }



  • 相关阅读:
    Eclipse/MyEclipse 选择Android NDK目录时提示“Not a valid NDK directory”
    Eclipse更改颜色主题
    Android模拟器访问本机服务器
    DIV水平垂直居中的CSS兼容写法
    Python3中使用PyMySQL连接Mysql
    Windows7 IE11 F12控制台DOC资源管理器报错的问题解决方法
    Windows 7无法卸载及安装IE11的解决方法
    查看端口占用
    VS2010/VS2013中ashx代码折叠的问题
    手机页面关于头部固定定位与input出现的问题
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2384418.html
Copyright © 2011-2022 走看看