Codeforces 990B :Micro-World

本文针对一个细菌吞噬问题,给出了两种不同的解决思路及实现代码。问题描述了在一个培养皿中有n个细菌,根据特定条件,一些细菌可以吞噬另一些细菌。文章通过排序和去重的方法求解最终可能剩下的最小数量的细菌。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 aiaj+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 aiaj+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 (1n21051≤n≤2⋅1051K1061≤K≤106) — number of bacteria and intergalactic constant KK.

The second line contains nn space separated integers a1,a2,,ana1,a2,…,an (1ai1061≤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;  
}  

转载于:https://www.cnblogs.com/Friends-A/p/9308995.html

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值