Micro-World (浅度学习unique函数的用法,菜,就别看了吧233~)

You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.

You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.

The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and aiaj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jj if ai>ajai>ajand aiaj+Kai≤aj+K. The swallow operations go one after another.

For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101,55,54][101,53,42,102,101_,55,54]  [101,53,42,102,55,54][101,53_,42,102,55,54]  [101,42,102,55,54][101_,42,102,55,54]  [42,102,55,54][42,102,55,54_]  [42,102,55][42,102,55]. In total there are33 bacteria remained in the Petri dish.

Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

input

The first line contains two space separated positive integers nn and KK (1n2105 1K106) — number of bacteria and intergalactic constant KK.The second line contains 

nn space separated integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106) — sizes of bacteria you have.

output

Print the only integer — minimal possible number of bacteria can remain.

题意:输入n和m,接下来n个数,步长为m,如果存在两个数之差为m,把小值删掉,问最终能剩几个数

example

7 1
101 53 42 102 101 55 54

  3

6 5
20 15 10 15 20 25

  1

7 1000000
1 1 1 1 1 1 1

  7

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int b[1000010],a[1000010];
int main()
{
    int n,m;
    memset(b,0,sizeof(b));
    scanf("%d%d",&n,&m);
    int i;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        b[a[i]]++;
    }
    sort(a,a+n);
    int l=unique(a,a+n)-a;//unique(a,a+n) - a返回的是去重后的数组长度
    int sum=n;
    for(i=1; i<l; i++)
    {
        if(a[i]>a[i-1]&&a[i]<=a[i-1]+m)
            sum-=b[a[i-1]];
    }
    printf("%d\n",sum);
return 0;
}

想看详细对unique解释的话可以看这个大佬的博客

我是链接,点我~

猜你喜欢

转载自blog.csdn.net/qq_42158522/article/details/80780635