(第七场)A Minimum Cost Perfect Matching 【位运算】
题目链接:https://www.nowcoder.com/acm/contest/145/A
A、Minimum Cost Perfect Matching
You have a complete bipartite graph where each part contains exactly n nodes, numbered from 0 to n - 1 inclusive.
The weight of the edge connecting two vertices with numbers x and y is x^y (bitwise AND).
Your task is to find a minimum cost perfect matching of the graph, i.e. each vertex on the left side matches with exactly one vertex on the right side and vice versa. The cost of a matching is the sum of cost of the edges in the matching.
denotes the bitwise AND operator. If you're not familiar with it, see {https://en.wikipedia.org/wiki/Bitwise_operation#AND}.
输入描述:
The input contains a single integer n (1 ≤ n ≤ 5e5).
输出描述:
Output n space-separated integers, where the i-th integer denotes p i (0 ≤ pi ≤ n - 1, the number of the vertex in the right part that is matched with the vertex numbered i in the left part. All pi should be distinct.
Your answer is correct if and only if it is a perfect matching of the graph with minimal cost. If there are multiple solutions, you may output any of them.
示例1:
输入
3
输出
0 2 1
说明 For n = 3, p0 = 0, p1 = 2, p2 = 1 works. You can check that the total cost of this matching is 0, which is obviously minimal.
题意概括:
求0~N-1的一个排列 p, 使得p[ i ] ^ i 的总和最小。
官方题解:
• 最优解的和一定是0。
• 当n是2的次幂的时候,非常简单p[i]=n-1-i即可。
• 否则,设b为<=n最大的2的次幂,
• 对于b <= x < n,交换x和x-b。
• 分别求两边的最优解。
举例
Minimum Cost Perfect Matching
• n = 11,初始为 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10
• 取b为8,开始交换 8, 9, 10, 3, 4, 5, 6, 7 / 0, 1, 2
• 取b为2,开始交换 8, 9, 10, 3, 4, 5, 6, 7 / 2, 1 / 0
• 翻转每段得到
• 7, 6, 5, 4, 3, 10, 9, 8 / 1, 2 / 0
解题思路:
题解给的是常识和构造。
最优和为 0 毋庸置疑。结合这道题思考,有按位与运算,我们可以从大到小把 i 取反(则相与的结果肯定为0),然后求该数 i 的最高位,保证取反的情况下不超过N的范围。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <bitset>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 5e5+;
int num[MAXN], a;
int N;
int hb(int k)
{
int n = ;
while(k > )
{
n++;
k>>=;
}
return n;
}
int main()
{
scanf("%d", &N);
bitset<> b;
memset(num, -, sizeof(num));
for(int i = N-; i >= ; i--)
{
if(num[i] == -)
{
b = ~i;
int len = hb(i);
int t = ;
int sum = ;
for(int k = ; k < len; k++)
{
sum+=b[k]*t;
t*=;
}
num[i] = sum;
num[sum] = i;
}
}
for(int i = ; i < N; i++)
{
printf("%d", num[i]);
if(i < N-) printf(" ");
}
puts("");
return ;
}
(第七场)A Minimum Cost Perfect Matching 【位运算】的更多相关文章
- 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)
题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...
- 牛客第七场 Minimum Cost Perfect Matching 规律
题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...
- 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】
链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- Minimum Cost(最小费用最大流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- [Swift]LeetCode1000. 合并石头的最低成本 | Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
随机推荐
- centos7 中文乱码问题解决方法
1.查看是否安装中文包 可以使用下面的命名查看系统是否安装了中文安装包. locale -a |grep "zh_CN" 没有输出,说明没有安装,输入下面的命令安装: yum gr ...
- Android ContentProvider的介绍(很详细)
博客分类: android进阶 一.ContentProvider的概念 ContentProvider:为存储和获取数据提供统一的接口.可以在不同的应用程序之间共享数据.Android已经为常见 ...
- 九度oj题目1181:遍历链表
题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2600 解决:1125 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...
- bzoj 5305: [Haoi2018]苹果树
Description Solution \(n\) 个点的二叉树的方案数是 \(n!\) 证明十分显然:新加入的点占掉了 \(1\) 个位置,新加了 \(2\) 个位置,那么多出来一个位置,所以第 ...
- 线程中断方法interrupt() 与 cancel()
(一).关于interrupt() interrupt()并不直接中断线程,而是设定一个中断标识,然后由程序进行中断检查,确定是否中断. 1. sleep() & interr ...
- ASP .NET SVN && emmet 插件
学习 ASP .NET 时间的第三周: 来讲讲如何在 visual studio 2013...上搭载 SVN吧: 废话不多说: One Step: 电脑上已安装 visual studio 201 ...
- /Date(1410019200000+0800)/如何转换为date对象
<script type="text/javascript">var s = '/Date(1410019200000+0800)/ '; s.replace(/Dat ...
- 在List中常用的linq表达式
为了下面举例方便,先声明一个集合: public List<Model.Resume> GetResumeList() { var list = new List<Model.Res ...
- 关于c3p0连接池连接mysql数据库需要注意的几点
什么是数据库连接池: 用池来管理Connection,这可以重复使用Connection.有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象. 当使用完Co ...
- 【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent
1.0 在helloworld项目基础上创建活动SecondActivity: 2.0 其中main.xml: <?xml version="1.0" encoding=&q ...