牛客第七场 Minimum Cost Perfect Matching 规律

题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式

分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反可以得到前面的一个值,这两个值的与运算结果为0

  不管奇数还是偶数,前面和后面的数都可以两两匹配,偶数刚好匹配,奇数还剩下一个0,0可以与0匹配结果还是0

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll vis[maxn], a[maxn];
ll f( ll x ) {
    string t = "";
    while(x) {
        char c = (x%2)+'0';
        t += c;
        x /= 2;
    }
    ll sum = 0;
    for( ll i = t.length()-1; i >= 0; i -- ) {
        ll tmp = t[i]-'0';
        sum = sum*2 + !tmp;
    }
    //debug(sum);
    return sum;
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    //f(1); f(2); f(3);
    ll n;
    while( cin >> n ) {
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        for( ll i = n-1; i >= 0; i -- ) {
            //debug(i);
            if( i == 0 && !vis[i] ) {
                a[i] = 0;
                continue;
            }
            if( !vis[i] ) {
                a[i] = f(i);
                //debug(f(i));
                vis[f(i)] = i;
            } else {
                a[i] = vis[i];
            }
        }
        for( ll i = 0; i < n; i ++ ) {
            if( i == n-1 ) {
                cout << a[i] << endl;
            } else {
                cout << a[i] << " ";
            }
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/l609929321/p/9450305.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值