990B - Micro-World

B. Micro-World
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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>aj and 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 are 33 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 (1n21051≤n≤2⋅1051K1061≤K≤106) — 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.

Examples
input
Copy
7 1
101 53 42 102 101 55 54
output
Copy
3
input
Copy
6 5
20 15 10 15 20 25
output
Copy
1
input
Copy
7 1000000
1 1 1 1 1 1 1
output
Copy
7
Note

The first example is clarified in the problem statement.

In the second example an optimal possible sequence of swallows is: [20,15,10,15,20,25][20,15,10,15,20_,25]  [20,15,10,15,25][20,15,10,15_,25]  [20,15,10,25][20,15,10_,25]  [20,15,25][20,15_,25]  [20,25][20_,25]  [25][25].

In the third example no bacteria can swallow any other bacteria.


题意:你有一个培养皿,里面n个细菌,每个细菌有自己的大小,大的细菌能吃掉小的细菌,自身大小不变,前提是大细菌只能吃掉小细菌加k系数大于大细菌才行。这句话就是条件  only if ai>ajai>aj and aiaj+K,求出培养皿中最少还有多少细菌。

题解:贪心  排个序后,然后依次比较小细菌是否加k大于大的细菌,满足的话,就总个数--。

#include<bits/stdc++.h>
using namespace std;
int n,k,a[200100],j;
int main()
{
    cin>>n>>k;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    int ans=n;
    for(int i=0; i<n; i++)
        while(a[j]<a[i])
        {
            if(a[i]<=a[j]+k) ans--;
            j++;
        }
    cout<<ans<<endl;
    return 0;
}

ai≤aj+K.这句话就是条件


n,k=map(int,input().split())
a=sorted(map(int,input().split()))
j=0
for i in a:
    while a[j]<i:
        if i<=a[j]+k:
            n-=1
        j+=1
print(n)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/80736220
今日推荐