CodeForces 990B

Posted daybreaking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 990B相关的知识,希望对你有一定的参考价值。

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 (1n2?1051≤n≤2?105, 1K1061≤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.

Sample Input

Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7

Hint

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.

 

题意:满足题中所给条件的病菌会发生吞并,问最后剩下的病菌的最少的数量。

吞并的条件有两个,可以通过在先确定一个条件的前提下,判断另一个条件。

先对病菌从大到小排一遍序,然后从后向前遍历一遍,只需要判断相邻的两个是否满足条件即可(特殊的是,有相等的数的时候),因为相邻的两个是差最小的,最容易满足第二个条件。

 

技术分享图片
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<stack>
 8 #include<deque>
 9 #include<map>
10 #include<iostream>
11 using namespace std;
12 typedef long long  LL;
13 const double pi=acos(-1.0);
14 const double e=exp(1);
15 const int N = 200010;
16 
17 #define lson i << 1,l,m
18 #define rson i << 1 | 1,m + 1,r
19 
20 map<LL,LL> mp;
21 LL con[N];
22 
23 bool cmp(LL a,LL b)
24 {
25     return a>b;
26 }
27 int main()
28 {
29     LL i,p,j,x;
30     LL n,k;
31     scanf("%lld%lld",&n,&k);
32     for(i=0;i<n;i++)
33     {
34         scanf("%lld",&con[i]);
35         mp[con[i]]++;
36     }
37     sort(con,con+n,cmp);
38     p=n;
39     for(i=n-2;i>=0;i--)
40     {
41         if(con[i]>con[i+1]&&con[i]<=(con[i+1]+k))
42         {
43             p=p-mp[con[i+1]];
44         }
45     }
46     printf("%lld
",p);
47     return 0;
48 }
View Code

 




以上是关于CodeForces 990B的主要内容,如果未能解决你的问题,请参考以下文章

CF 990B B. Micro-World数组操作/贪心/STL/二分搜索

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)

(Incomplete) Codeforces 394 (Div 2 only)