传送门
题目描述
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 ai≤aj+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 i can swallow any bacteria j if ai>aj and ai≤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 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.
题目翻译(摘自luogu)
给出一段长度为n的初始数列,当a[i]>a[j]并且a[i]<=a[j]+k时,可以将a[j]删除。求最终数列可能的最小长度
输入:
第一行输入n,k
第二行输入n个数表示a[i]
输出:
最终最小的可能长度
感谢@zhaotiensn 提供翻译
解题思路
先排序,然后扫一遍。
水题。
代码
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 int n,k; 7 int a[500000]; 8 inline void read(register int &x){ 9 x=0; register char ch=getchar(); 10 while(ch<'0'||ch>'9')ch=getchar(); 11 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); 12 } 13 int main(){ 14 read(n),read(k); 15 for(register int i=1;i<=n;i++){ 16 read(a[i]); 17 } 18 sort(a+1,a+n+1); 19 int ans=n; 20 for(register int i=1;i<n;i++){ 21 int num=a[i],pos=i; 22 while(a[i+1]==a[i])i++; 23 int ne=a[i+1]; 24 //cout<<num<<' '<<ne<<endl; 25 if(ne>num&&ne<=num+k)ans-=(i-pos+1); 26 } 27 cout<<ans<<endl; 28 }
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 ai≤aj+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 i can swallow any bacteria j if ai>aj and ai≤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 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.