2021牛客暑期多校训练营10 H War of Inazuma (Easy Version)

War of Inazuma (Easy Version)
题目链接:

https://ac.nowcoder.com/acm/contest/11261/H

题目描述:

As a traveler, you once witnessed the war of Inazuma, of which sides are the resistance and the Shogun’s army.

The map of Inazuma can be viewed as a unit n-dimensional hypercube, which has exactly 2^n vertices. Each vertex is labeled with an integer from 0 to 2^n - 1. Two vertices are adjacent if and only if there exists exactly one different bit in their nn-bit binary representation.

In the war of Inazuma, some vertices were occupied by the resistance army, and others were occupied by the Shogun’s Army. You also noticed that for each vertex u, the number of vertices adjacent to u and occupied by the same side as uu was no more than ⌈ sqrt n ⌉.

Many years later, you have already forgotten details of the war. Can you construct a hypercube satisfying all the above requirements?

输入描述:

The only line contains a single integer n(1≤n≤22).

输出描述:

Output a 01-string of length 2^n . The a1…an-th bit describes which side is vertex (a1,…,an) occupied by, 0 for the resistance army and 1 for the Shogun’s army. Here a1…an is an n-bit binary representation. Any answer satisfying all requirements will be accepted.

示例1:

输入
2
输出
0111

代码如下:
#include<iostream>
using namespace std;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<(1<<n);i++)
		cout<<(__builtin_popcount(i)&1);  //返回输入数据二进制中1的个数
	return 0;
}
### 2024 暑期训练营 Sort4 题目解析 #### 题目背景与描述 在2024年的暑期训练营中,Sort4是一道涉及字符串排序和字典序比较的题目。该题目的核心在于通过特定的操作改变给定字符串数组的顺序,并最终使得整个序列达到某种最优状态。 #### 解决方案概述 为了有效解决这个问题,需要理解并应用分治算法以及RMQ(Range Minimum Query)技术来优化查询效率[^3]。具体来说: - **初始化阶段**:读入输入数据并将所有字符串存储在一个列表中。 - **预处理部分**:构建笛卡尔树用于快速查找最小值及其位置;同时记录每个节点的信息以便后续更新操作时能够高效定位目标元素的位置。 - 对于每一次交换请求\( (d_i, p_i) \),判断当前待调整项是否满足条件 \( d_i > p_i \)。如果是,则执行相应的移动动作使新的排列更接近理想解; - 使用自定义比较器对修改后的集合重新排序,确保整体结构仍然保持有序特性不变; - 继续上述过程直至完成全部指令集中的每一条命令为止。 ```cpp #include <bits/stdc++.h> using namespace std; struct Node { int val; int idx; }; bool cmp(Node a, Node b){ return a.val < b.val || (a.val == b.val && a.idx < b.idx); } vector<Node> nodes; int n, q; void buildCartesianTree() {/* ... */ } // 执行单步转换操作 void performOperation(int di, int pi) { if(di > pi){ // 当di大于pi时才会引起字典序变化 swap(nodes[di],nodes[pi]); sort(nodes.begin(), nodes.end(),cmp); } } ``` 此段代码展示了如何基于给定条件实施一次有效的变换,并维持全局秩序不受影响。需要注意的是,在实际竞赛环境中可能还需要额外考虑边界情况以及其他潜在陷阱。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值