概述
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 jj if 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.
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.
Print the only integer — minimal possible number of bacteria can remain.
7 1 101 53 42 102 101 55 54
3
6 5 20 15 10 15 20 25
1
7 1000000 1 1 1 1 1 1 1
7
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.
题解:整体扫一遍,用二分查找是否有符合条件的。刚开始开1e6的数组打表,结果超时了,额....。
笔记:
#include<algorithm> 中
lower_bound()为大于等于n的第一个数的位置
upper_bound()为大于n的第一个数的位置
参考博客
#include<functional> 中
greater<int>()函数即可对逆序求最近位置
注意:还有一个作用,可以在优先队列里使用,使其正序。
(其实,用cmp也可以)
在逆序排序的时候可以直接用。
#include<bits/stdc++.h>
using namespace std;
int a[1000000+100];
int main()
{
int n,m,sum=0;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n,greater<int>());
for(int i=0;i<n;i++)
{
int cc=a[i]+m;
int aum=lower_bound(a,a+n,cc,greater<int>())-a;
if(a[aum]>a[i])
{
sum++;
}
}
cout<<n-sum<<endl;
return 0;
}
最后
以上就是爱笑洋葱为你收集整理的Codeforces B. Micro-World的全部内容,希望文章能够帮你解决Codeforces B. Micro-World所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复