Educational Codeforces Round 85 B. Middle Class (sort / greedy / water)

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 
Can be considered greedy, only need to exceed x or greater than or equal to x to be rich, so why waste more money? If a person's money exceeds x, first ++ ans, then add the excess money to the variable sum, and finally sort by the money from small to large, scan the array from back to front, for people who are less than x Can sum be used to reach x. (In order to ensure as many people as possible, first meet the person with a small gap between the amount of money and x)
Note: It's guaranteed that the total sum of n n doesn't exceed 10 5 105. So this complexity is no problem.
#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 stores the amount of money over 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)
                {++ 
                    years ;
                    sum-=(x-a[i]);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
 } 

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12678567.html