• Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)


    Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles.

    The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that:

    • the government chooses some subset of people (maybe all of them);
    • the government takes all savings from the chosen people and redistributes the savings among the chosen people equally.

    For example, consider the savings as list [5,1,2,1][5,1,2,1] : if the government chose the 11 -st and the 33 -rd persons then it, at first, will take all 5+2=75+2=7 burles and after that will return 3.53.5 burles to the chosen people. As a result, the savings will become [3.5,1,3.5,1][3.5,1,3.5,1] .

    A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.

    Input

    The first line contains single integer TT (1T10001≤T≤1000 ) — the number of test cases.

    Next 2T2T lines contain the test cases — two lines per test case. The first line contains two integers nn and xx (1n1051≤n≤105 , 1x1091≤x≤109 ) — the number of people and the minimum amount of money to be considered as wealthy.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109 ) — the initial savings of each person.

    It's guaranteed that the total sum of nn doesn't exceed 105105 .

    Output

    Print TT integers — one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.

    Example
    Input
    Copy
    4
    4 3
    5 1 2 1
    4 10
    11 9 11 9
    2 5
    4 3
    3 7
    9 4 9
    
    Output
    Copy
    2
    4
    0
    3
    可以贪心地考虑,只需要超过x或者大于等于x就是富有的,那么何必多浪费钱呢?如果一个人的钱数超过x,就先++ans,再把超过的钱累加到变量sum里,最后按照钱数从小到大排序,从后往前扫一遍数组,对于不足x的人看看能否用sum使之钱数达到x。(为保证人数尽可能多,先满足钱数与x差距小的人)
    注意:It's guaranteed that the total sum of nn doesn't exceed 105105. 所以这个复杂度是没问题的。
    #include <bits/stdc++.h>
    using namespace std;
    int n,x,a[100005];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            scanf("%d%d",&n,&x);
            int i;
            int ans=0; 
            long long sum=0;//sum存储的是超过x的钱数 
            for(i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                if(a[i]>=x)ans++,sum+=a[i]-x;
            }
            sort(a+1,a+n+1);
            for(i=n;i>=1;i--)
            {
                if(sum<=0)break;
                if(a[i]<x)
                {
                    if(x-a[i]<=sum)
                    {
                        ans++;
                        sum-=(x-a[i]);
                    }
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
     } 
  • 相关阅读:
    docker基础总结
    python基础学习总结
    静默(命令行)安装oracle 11g
    java中如果函数return可能抛出异常怎么办
    Android 开发先驱的博客列表
    栈与队列
    线性表
    算法
    数据结构概论
    iOS开发中实现手势解锁
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12678567.html
Copyright © 2020-2023  润新知