(sorting+贪心)打cf之路->micro-world

http://codeforces.com/problemset/problem/990/B
这题是sorting+贪心,怪不得TE了惹QAQ
You know that you havenbacteria in the Petri dish and size of thei-th bacteria isai. Also you know intergalactic positive integer constantK

The i-th bacteria can swallow thej-th bacteria if and only ifai>ajandai≤aj+K. Thej-th bacteria disappear, but thei-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteriaican swallow any bacteriajifai>ajandai≤aj+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 swallow 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.

Input

The first line contains two space separated positive integers n and K(1≤n≤2⋅105,1≤K≤106) — number of bacteria and intergalactic constant K

The second line contains space separated integers a1,a2,…,an(1≤ai≤106) — sizes of bacteria you have.

Output

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

example:[20,15,10,15,20–––,25]→[20,15,10,15–––,25]→[20,15,10–––,25]→[20,15–––,25]→[20–––,25]→[25].

以下是正确的(抄来的)代码

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n, k, a[200001], i, t = 0, j;
    cin >> n >> k;
    for (i = 0; i<n; i++)
    {
        cin >> a[i];
    }
    sort(a, a + n);
    for (i = 0; i<n - 1; i = j)
    {
        for (j = i + 1; j<n&&a[j] == a[i]; j++);//数字相同的都是会被吞的
        if ((a[j] <= a[i] + k) && a[j]>a[i])
        {
            t = t + j - i;//有多少个被吞
        }
    }
    cout << n - t;//剩下没被吞的
    return 0;
}

我还是一脸懵逼,留着先

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,871评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,973评论 3 20
  • 今天中秋,楼下一位妈妈带着孩子在赏月吗?孩子奶声奶气地念着古诗:床前明月光,疑是地上霜,举头望明月,低头思故乡。一...
    木木sani阅读 352评论 0 0
  • “根本就没有食神。” “或者人人都是食神。” “老爹老妈大哥小妹凯子马子,只要用心,人人都可以是食神。” 在周星驰...
    黄青海阅读 1,162评论 20 24
  • 怎么说呢,似乎在小说里,在别人的嘴里,青春,就是兵荒马乱的盛宴,但是,讲真,我觉得我的青春并没有很热血,恰恰...
    亦又阅读 183评论 0 0

友情链接更多精彩内容