zoukankan      html  css  js  c++  java
  • 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A
    来源:牛客网
     

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 262144K,其他语言524288K
    Special Judge, 64bit IO Format: %lld

    题目描述

    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 (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 ≤ 5 * 105).

    输出描述:

    Output n space-separated integers, where the i-th integer denotes pi (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.

    刚开始看题解不知道他在讲什么.......

    后来和czc讨论了一下总算是明白为什么了....

    显然结果会是0 题目主要是去找一个序列使得这个对应的&都是0

    如果n是2的次幂 那么倒一下顺序就正好了 因为他们的0和1是对称的

    如果n不是2的次幂 那么我们就找到小于n的 最大的2的次幂x

    把【x~n-1】的数和【0~n-x-1】的数一一交换 因为这些数只有最高位不同

    而且只有【x~n-1】的数最高位是1 要先把他们解决掉 解决的方式就是找最高位是0的 低位的解决方式就相当于n是2的次幂时

    那么对于被换到后面的那些数来说 继续按照上面的方法

    他们的个数是len 分成len是2的次幂和len不是2的次幂 

    不断递归

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int n;
    const int maxn = 5 * 1e5 + 5;
    int num[maxn];
    
    void solve(int len, int pos)
    {
        int t = len, k = 0;
        while(t){
            k++;
            t /= 2;
        }
        k--;
    
        int x = pow(2, k);
        if(len - x > 0){
            for(int i = 0; i < len - x; i++){
                swap(num[pos + i], num[pos + x + i]);
            }
            for(int i = 0; i < x / 2; i++){
                swap(num[pos + i], num[pos + x - i - 1]);
            }
            solve(len - x, pos + x);
        }
        else{
            for(int i = 0; i < x / 2; i++){
                swap(num[pos + i], num[pos + x - i - 1]);
            }
            return;
        }
    }
    
    int main()
    {
    	while(scanf("%d", &n) != EOF){
            for(int i = 0; i < n; i++){
                num[i] = i;
            }
    
            solve(n, 0);
            printf("%d", num[0]);
            for(int i = 1; i < n; i++){
                printf(" %d", num[i]);
            }
            printf("
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    基础语法;
    layabox里面的ui组件之RadioGroup
    github删除带有文件的文件夹
    【转】NHibernate:no persister for 异常
    MVC乱码可能的原因
    【转】局域网内访问VS2012 调试的IIS Express web服务器
    Hightchart y轴不允许显示小数
    WCF 断点不会命中
    Web Form 取消手机端自动转换
    Sharepoint的javascript客户端对象模型获取其他站点的list
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643381.html
Copyright © 2011-2022 走看看