Educational Codeforces Round 45 (Rated for Div. 2) 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 n bacteria in the Petri dish and size of the i - th bacteria is ai. Also you know intergalactic positive integer constant K.

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

For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54] and K=1. The one of possible sequences of swallows is: [101,53,42,102,101_,55,54] [101,53_,42,102,55,54]  [101_,42,102,55,54]  [42,102,55,54_]    [42,102,55]. In total there are 3 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 n and K (1n21051K106) — number of bacteria and intergalactic constant K.

The second line contains n space separated integers a1,a2,,an (1ai106) — 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.

题目:给一串数列,ai < aj && ai + k >= aj那么就把ai删掉,这样就直接记录每个数字的个数,然后贪心算一下就可以啦,注意只要后面k个数存在就去掉!!!

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e6+7;
ll n,k, a[maxn], sum[maxn];
int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%lld%lld", &n, &k))
    {
        ll ms = 0, ans = 0;
        memset(a, 0, sizeof(a));
        memset(sum, 0, sizeof(sum));
        while(n--) {
            ll c; scanf("%lld", &c); ms = max(ms, c); a[c]++;
        }
        sum[0] = a[0];
        for(int i = 1; i <= ms+k; i++) sum[i] += (a[i] + sum[i - 1]);

        for(int i = 1; i <= ms; i++) {
            if(a[i]!=0) {
                ll t = sum[i+k] - sum[i];
                //cout << t << endl;
                if(t==0) {
                    ans += a[i];
                }
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_39792252/article/details/80652912