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 ai≤aj+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 jjif ai>ajai>aj and ai≤aj+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 (1≤n≤2⋅1051≤n≤2⋅105, 1≤K≤1061≤K≤106) — number of bacteria and intergalactic constant KK.

The second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤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个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且ai<=aj+k,那么细菌i就可以吃掉细菌j,问最后可以剩于多少个细菌。

我的思路:对数组a进行降序排序,并记录下来次大值出现的位置,将最大值出现的个数记为j,j-1得到无法进行吞噬的细胞数,即最后剩下的细胞中一定会包括所有的尺寸最大的细胞。将排好序的细胞从n位置开始去重。

如果所有的细胞尺寸相同,最终结果等于n。如果尺寸不全相同,遍历去重后的数组,查找有多少相邻元素不符合要求,更新ans的值

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int b[maxn];
bool cmp(int x,int y)
{
return x>y;
}
int main(int argc, char const *argv[])
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
int x=0;
int ans=0;
memset(b,0,sizeof(b));
b[x]=a[0];
int flag=1;
int j;
for(j=1;;j++)
{
if(a[0]==a[j])
ans++;
else
break;
}//查找次大值出现的位置和最大值出现的次数
for(int i=j;i<n;i++)
{
if(a[i]==b[x])
flag+=1;
if(a[i]==b[x]&&abs(a[i]-b[x-1])>k)
{
ans++;
}
else
b[++x]=a[i];
}//对数组进行去重
if(flag==n)
ans=n;//如果所有元素相同,ans=n
else
{//否则,遍历数组
for(int i=0;i<x;i++)
{
if(b[i]-b[i+1]>k)
ans++;
}
ans++;
}
cout<<ans<<endl;
return 0;
}

另一种做法(百度上找的,比我写的简单了好多):用一个数组记录相同尺寸细菌的个数,然后进行去重排序,从次小的细胞开始比较相邻的细胞是否符合要求,如果符合要求,减去较大尺寸细胞的个数

原帖地址:点击打开链接

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int b[maxn];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
}
sort(a,a+n);
int len=unique(a,a+n)-a;
int ans=n;
for(int i=1;i<len;i++)
if(a[i]>a[i-1]&&a[i]<=a[i-1]+k)
ans-=b[a[i-1]];
cout<<ans<<endl;
return 0;
}

Codeforces 990B :Micro-World的更多相关文章

  1. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  2. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  3. Codeforces 747C:Servers(模拟)

    http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...

  4. Codeforces 749D:Leaving Auction(set+二分)

    http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...

  5. Codeforces 749B:Parallelogram is Back(计算几何)

    http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...

  6. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

  7. Codeforces 746D:Green and Black Tea(乱搞)

    http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...

  8. Codeforces 745C:Hongcow Builds A Nation(并查集)

    http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...

  9. Codeforces 743D:Chloe and pleasant prizes(树形DP)

    http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...

随机推荐

  1. Ubuntu 系统下暴力卸载 MySQL

    一.概述 MySQL 出问题了,正常的 start.stop 不起作用. apt-get remove mysql-server apt-get remove mysql-client 上面这些命令不 ...

  2. js中 a : function(){}这是什么格式? 代表什么含义?怎样学习这样的格式?

    js中的json. 一种轻量级数据格式.json中的值是map形式的就是key->value. 具体看下边的示例; var person = { // 用 大括号括声明一个json. " ...

  3. EditPlus 4.3.2463 中文版已经发布(10月16日更新)

    距离上个版本在本年5月发布后,EditPlus 网站沉寂多月.日前终于发布了一个新的小版本. 该版本却具有多项改进,值得一提: * Ctrl+Alt+Up/Down 键在列选模式下可插入多个插入点. ...

  4. EF Code First 学习笔记:约定配置(转)

      要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一 ...

  5. PHP curl模拟浏览器采集阿里巴巴的实现代码

    <?php set_time_limit(0); function _rand() { $length=26; $chars = "0123456789abcdefghijklmnop ...

  6. 怎样快速掌握一个用你没学过的框架写的PHP项目?

    我的思路一般是先搞定框架的route.也就是说,明白一个请求的url地址是对应的哪个controller处理的,找到controller后,再理解一下它的类库加载方案,也就是说一些辅助类以及自己逻辑类 ...

  7. tomcat的安装和启动

    下载apache-tomcat-8.5.5-src,我将其放在了/usr/local/tomcat目录下 要启动需要运行: /usr/local/tomcat/apache-tomcat-8.5.5- ...

  8. shell 输入不显示在监视器上

    #!/bin/bash read -s -p "Enter your password:" pass echo "your password is $pass" ...

  9. Android 引用资源

    比如在 strings.xml 中找到的 Hello world!字符串,我们有两种方式可以引用它: 1. 在代码中通过 R.string.hello_world 可以获得该字符串的引用: 2. 在 ...

  10. SQL Server 查询优化 索引的结构与分类

    一.索引的结构 关系型数据库中以二维表来表达关系模型,表中的数据以页的形式存储在磁盘上,在SQL SERVER中,数据页是磁盘上8k的连续空间,那么,一个表的所有数据页在磁盘上是如何组织的呢?分两种情 ...