#132. “与”

博客围绕一个算法题目展开,给定长度为n的序列A,要求找出一对Ai、Aj(1<=i<j<=n)使Ai与Aj的位运算结果最大。介绍了输入、输出要求,给出样例说明,还提及时间和数据范围,并给出了一个暴力算法代码。

【题目描述】:

给你一个长度为n的序列A,请你求出一对Ai,Aj(1<=i<j<=n)使Ai“与”Aj最大。

Ps:“与”表示位运算and,在c++中表示为&。

【输入描述】:

第一行为n。接下来n行,一行一个数字表示Ai。

【输出描述】:

输出最大的Ai“与”Aj的结果。

【样例输入】:

3
8
10
2

【样例输出】:

8

【样例说明】:

8 and 10 = 8

8 and 2 = 0

10 and 2 = 2

【时间限制、数据范围及描述】:

时间:1s 空间:64M

20%的数据保证n<=5000

100%的数据保证 n<=3*10^5,0<=Ai<=10^9

这就是一个关于与运算(&)的题目。

先来一个暴力code:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>

using namespace std;
const int N=300005;
int a[N];

int read()
{
	int ans=0,f=1;
	char c;
	c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
		f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans*f;
}
int main()
{
	int maxx=0; 
	int n;
	n=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
		//	printf("%d&%d:%d ",a[i],a[j],a[i]&a[j]);
			maxx=max(maxx,a[i]&a[j]);
		}
	}
	printf("%d",maxx);
	return 0;
}

以上就是一个朴素到不能再朴素的算法了....(讲道理我认为位运算还是蛮快的,一个比效率最低的模运算要好多啦,依旧不知道为什么速度似乎不是太快....) 

#include<stdio.h>
#include<iostream>
#include<math.h> 
# define Maxn 300005
using namespace std;
int f[Maxn],tot[35];
int read()
{
	char c;
	int ans=0,f=1;
	c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
		{
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans*f;
}
int main()
{
	freopen("51.in","r",stdin);
	freopen("51.out","w",stdout);
	int n,now,ans=0;
	n=read();
	now=n;
	for(int i=1;i<=n;i++)
	f[i]=read();
	for(int i=30;i>=0;i--)
	{
		int ok=0;
		for(int j=1;j<=now;j++)
		if(f[j]&(1<<i))ok++;
		if(ok<2)
		continue;
		int k=1;
		tot[i]=1;
		for(int j=1;j<=now;j++)
		if(f[j]&(1<<i))
		f[k++]=f[j];
		now=k-1;
	}
		for(int i=30;i>=0;i--)
		{
			if(tot[i])
			ans=ans^(1<<i);
		}
	printf("%d\n",ans);
	return 0;
}

 

 

 

 

# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please consult the documentation for further information on configuration options: # https://www.elastic.co/guide/en/elasticsearch/reference/index.html # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: MyES # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: node.name: master node.master: true node.data: true # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # path.data: /var/lib/elasticsearch # # Path to log files: # path.logs: /var/log/elasticsearch # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # # Make sure that the heap size is set to about half the memory available # on the system and that the owner of the process is allowed to use this # limit. # bootstrap.memory_lock: false bootstrap.system_call_filter: false # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network.host: 192.168.100.130 # # Set a custom port for HTTP: # http.port: 9200 http.cors.enabled: true http.cors.allow-origin: "*" # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when new node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # #discovery.zen.ping.unicast.hosts: ["host1", "host2"] discovery.zen.ping.unicast.hosts: ["192.168.100.130", "192.168.100.131","192.168.100.132"] # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1): discovery.zen.minimum_master_nodes: 3 #discovery.zen.minimum_master_nodes: # # For more information, consult the zen discovery module documentation. # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # #gateway.recover_after_nodes: 3 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true 这是我的master里的配置,请帮我配置slave1和slave2
最新发布
05-23
### 配置 Elasticsearch 集群中的 Slave1 和 Slave2 节点 在配置 Elasticsearch 集群时,Slave 节点主要用于处理数据存储和查询请。以下是基于已有 Master 节点配置的情况下,如何设置 Slave1 和 Slave2 节点的详细说明。 #### 1. **基本配置项解释** - **`cluster.name`**: 确保所有节点属于同一个集群,因此需要 Master 节点保持一致。 - **`node.name`**: 每个节点都需拥有唯一的名称,以便区分不同节点的功能。 - **`node.master`**: 设置为 `false` 表明该节点不会参主节点选举过程[^3]。 - **`node.data`**: 设定为 `true`,表明此节点能够承担实际的数据存储功能[^2]。 - **`network.host`**: 定义当前节点监听的网络地址,通常设为本机 IP 地址或特定网卡地址。 - **`discovery.seed_hosts` / `cluster.initial_master_nodes`**: 声明哪些主机可以作为潜在的 Master 节点,确保新加入的节点能找到集群中的 Master[^1]。 #### 2. **具体配置示例** 对于 Slave1 和 Slave2 的配置文件(通常是 `/etc/elasticsearch/elasticsearch.yml`),可以根据以下模板进行编辑: ##### **Slave1 配置** ```yaml # 集群名称,必须 Master 节点相同 cluster.name: your_cluster_name # 当前节点名称,唯一标识符 node.name: slave1 # 不允许竞选为主节点 node.master: false # 允许存储数据 node.data: true # 绑定到本地或其他指定IP地址 network.host: 192.168.x.x # 替换为实际IP地址 # HTTP服务端口,默认即可 http.port: 9200 # Transport协议端口,默认即可 transport.tcp.port: 9300 # 发现机制中列出已知的Master节点 discovery.seed_hosts: ["master_node_ip", "another_master_node_ip"] # 初始主节点列表 cluster.initial_master_nodes: ["master_node_name", "another_master_node_name"] ``` ##### **Slave2 配置** 除了更改 `node.name` 外,其余部分几乎完全一样: ```yaml # 集群名称,必须 Master 节点相同 cluster.name: your_cluster_name # 当前节点名称,唯一标识符 node.name: slave2 # 不允许竞选为主节点 node.master: false # 允许存储数据 node.data: true # 绑定到本地或其他指定IP地址 network.host: 192.168.y.y # 替换为实际IP地址 # HTTP服务端口,默认即可 http.port: 9200 # Transport协议端口,默认即可 transport.tcp.port: 9300 # 发现机制中列出已知的Master节点 discovery.seed_hosts: ["master_node_ip", "another_master_node_ip"] # 初始主节点列表 cluster.initial_master_nodes: ["master_node_name", "another_master_node_name"] ``` #### 3. **启动前准备事项** - **内存映射调整**:为了避免 JVM 报错,建议预先提升系统的最大内存映射数量。可以通过命令行临时修改或永久生效的方式完成此项工作: ```bash sysctl -w vm.max_map_count=262144 ``` - **Docker 网络环境**:如果是在 Docker 下运行,则需要提前创建好容器间的共享网络环境[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值