Hdu_6095_ thinking problem _ greedy problem

Source: Internet
Author: User

Rikka with competition

As we know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the them:

A wrestling match would be held tomorrow. N players would take part in it. The ith player ' s strength is AI.

If There is a match between the ith player plays and the jth player, the result would be related to |ai−aj|. If |ai−aj|>k, the player with the higher strength point would win. Otherwise Each player has a chance to win.

The competition rules is a little strange. Each time, the referee would choose the players from all remaining players randomly and hold a match between them. The loser would be eliminated. After n−1 matches, the last player would be the winner.

Now, Yuta shows the numbers n,k and the array A and he wants to know how many players has a chance to win the competition .

It is too difficult for Rikka. Can you help her?
Input
The first line contains a number T (1≤t≤100), the number of the testcases. And there is no more than 2 testcases with n>1000.

For each testcase, the first line contains the numbers n,k (1≤n≤105,0≤k<109).

The second line contains n numbers ai (1≤ai≤109).
Output
For each testcase, print a single line with a single number–the answer.
Sample Input
2
5 3
1 5 9) 6 3
5 2
1 5 9) 6 3
Sample Output
5
1
Source
Multi-university Training Contest-team 5

Any two people compared to the difference is greater than K, sub-low elimination, otherwise two people are likely to win, the remaining continue than, ask how many people may win?

From the reverse, from small to large platoon, the difference between <=k, there is a chance to win, otherwise the rest will be eliminated;

 #include <iostream> #include <cstring> #include <cstdio> #include <
Algorithm> using namespace std;
typedef long Long LL;
ll num[(int) 1e5+10];
    int main () {int t;
            while (cin>>t) {while (t--) {ll n,k,i;
            cin>>n>>k;
            for (i=0; i<n; i++) scanf ("%lld", num+i);
           Sort (num,num+n);
            for (i=0;i<n;i++)//cout<<num[i]<<endl;
            ll Ans=1;
                for (i=n-1; i>0; i--) {//puts ("1111");
                    if (num[i]-num[i-1]<=k) {//puts ("%%%%%");
                ans++;
            } else break;
        } cout<<ans<<endl;
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.