牛客2018年第七次acm暑期培训----A-Minimum Cost Perfect Matching

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

Minimum Cost Perfect Matching

时间限制: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.
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=500100;
int d[MAXN];
int c[20];
int main()
{
    int n,i,j;
    c[0]=1;
    for(int i=0;i<20;i++)
    {
        c[i+1]=2<<i;
    }
    scanf("%d",&n);
    int p=upper_bound(c,c+20,n-1)-c;
    if(p!=0)p--;
    for(int i=n-1;i>=0;i--)
    {
        if(i!=0&&d[i]==0)
        {
            while(i<c[p])p--;
            d[i]=2*c[p]-i-1;
            d[2*c[p]-i-1]=i;
        }
    }
    for(int i=0;i<n-1;i++)
        printf("%d ",d[i]);
    printf("%d\n",d[n-1]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值