HDU 2017 多校联赛5 1011 Rikka with Competition

Problem Description

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

A wrestling match will be held tomorrow. n players will take part in it. The ith player’s strength point is ai.

If there is a match between the ith player plays and the jth player, the result will be related to |ai−aj|. If |ai−aj|>K, the player with the higher strength point will win. Otherwise each player will have a chance to win.

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

Now, Yuta shows the numbers n,K and the array a and he wants to know how many players have 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 are no more than 2 testcases with n>1000.

For each testcase, the first line contains two 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


5 3 
1 5 9 6 3 
5 2 
1 5 9 6 3

Sample Output


1

题目大意:

摔跤比赛将于明天举行。运动员将参加。随着玩家的实力是AI。 
如果有与玩家之间,金天海球员比赛,结果将是| AI−AJ |相关。如果| AI−AJ |>K,与高强度点的玩家将赢得。否则,每个玩家将有机会赢得。 
比赛的规则有点奇怪。每一次,裁判都会随机从剩下的球员中选出两名球员,并在他们之间进行比赛。失败者将被淘汰。N−1场比赛后,最后的球员将是赢家。 
现在,Yuta显示数N、K和阵列,他想知道有多少玩家有机会赢得比赛。 
把 a_ia 
​i 
​​ 从大到小排序,那么第 ii 强人要获胜,最优情况下是最强的人输给了第二强的人,第二强的人输给了第三强的人,以此类推。因此只需要判断排序后 max_{j

c++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,k;
        int a[500001];
        cin>>n>>k;
        for (int i=0; i<n; i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        int ans=0;
        int flag=0;
        for (int i=n-1; i>0; i--)
        {
            if (abs(a[i]-a[i-1])<=k)
            {
                ans += 1;
                //cout<<"!!!!"<<endl;
            }
            else
            {
                ans += 1;
                flag=1;
                break;
            }
        }
            if (abs(a[0]-a[1])<=k&&flag==0)
                ans++;
        cout<<ans<<endl;
    }
}





全部评论

相关推荐

2025-12-02 02:15
门头沟学院
最近菊厂陆续开了,极力劝退那些拿13级的985硕士,就13级那么点儿薪资,一线城市每个月到手1.8/7/6w,租房2k还是破烂,吃饭2k还是预制菜,买个1k衣服都是聚酯纤维破塑料,稍微出去浪一浪,能留1w就是万岁,要是再有个啥都想买的对象,一线工作一年难存10w。隔壁工地混泥土,钳工,焊工一天800+,还包吃包住。读书18年到985硕士出来就为了进厂螺丝工?还不如从8岁童工开始干活,别人读书完了你工龄18+,混不上领导也是个小头头了。当然专科进来正式工,od都行,一般本科进来13级也OK,毕竟22岁年纪摆在那个地方还不需要太花钱。读硕博的基本26岁,工作两年就要结婚的,兜里没几个崽,连彩礼都要信用贷。菊厂离职的不少,毕竟正常没人受得了9116(梗:再来一次911刷6)。为啥这时候劝?因为刚下班,因为国考刚完,省考下周,就是可惜选调只有当年应届能报。现在回想能拍断大腿。应届生真实好身份,错过这一次,选调,考公,考编,当老师,进医院,研究所,高校,央国企,基本都无缘了,就连报名资格都被剥夺了,可谓是被党和国家遗弃的废材,统称“社会上的”,扔到社会去流浪,被用坏了就扔医院,长期超负载使用,零件修不好基本可以扔火里回炉重造了。体制内奉行找体制内的,都是党和国家选的人才,智力不差,样貌不丑,身材端正,收入稳定,安居房政策福利待遇也OK。因公出行都是报销,周末顺带“游山玩水“,这种体制内单身资源但凡想找对象,去社会上随便吆喝一声都排队。观察一下,基本没什么公务员在相亲,因为早就被邻里邻居抢光了。
哈哈哈,你是老六:就这不去的人大把人干呢,现在不缺人干活,你不干大把干呢,还有那个说农民工赚钱的,那个800+我估计肯定也就那一段时间,哪有这么赚钱,还是一句话,要想存下钱必须花销极低,能省的就不花钱,工资要高点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务