题目链接: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 【位运算】的更多相关文章

  1. 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)

    题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...

  2. 牛客第七场 Minimum Cost Perfect Matching 规律

    题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...

  3. 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  4. [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 ...

  5. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  6. Minimum Cost(最小费用最大流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  7. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  8. [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 ...

  9. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

随机推荐

  1. Apache Beam的架构概览

    不多说,直接上干货! Apache Beam是一个开源的数据处理编程库,由Google贡献给Apache的项目,前不久刚刚成为Apache TLP项目.它提供了一个高级的.统一的编程模型,允许我们通过 ...

  2. spring-data-elasticsearch使用笔记

    使用spring-data遇到了一些问题,记录一下. spring-data-elasticsearch版本选择 这里有一份官方github上的spring-data-elasticsearch与el ...

  3. 通过c++ 读写文本文件的中文乱码的解决方法

    前提:VS2010 ,MFC ,文本文件为ANSI格式. 读文件: CString str,fileContent;CStdioFile myFile, File;if(myFile.Open(Gen ...

  4. TOJ 1721 Partial Sums

    Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...

  5. java的访问修饰符

    Java中通过访问控制符(default,private,public,protected)来控制对类.变量.方法.构造方法的访问. 下表说明了4中修饰符的访问权限: 修饰符 当前类 同一包内 子孙类 ...

  6. MongoDB的MapReduce用法及php示例代码

    MongoDB虽然不像我们常用的mysql,sqlserver,oracle等关系型数据库有group by函数那样方便分组,但是MongoDB要实现分组也有3个办法: * Mongodb三种分组方式 ...

  7. HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  8. axios请求报Uncaught (in promise) Error: Request failed with status code 404

    使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...

  9. navicat 12 破解

    一.安装 官方下载下载 http://www.navicat.com.cn/download/navicat-premium 二.安装完后下载破解文件 https://pan.baidu.com/s/ ...

  10. Android滑动删除功能

    今天学习了新的功能那就是滑动删除数据.先看一下效果 我想这个效果大家都很熟悉吧.是不是在qq上看见过这个效果.俗话说好记性不如赖笔头,为了我的以后,为了跟我一样自学的小伙伴们,我把我的代码粘贴在下面. ...