Codeforces Round #177 (Div. 1)C. Polo the Penguin and XOR operation【贪心】

http://codeforces.com/contest/288/problem/C

按二进制位从大到小进行匹配

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int a[1000100] , n , m , y , x , l;
ll r = 0;
int main() {
    cin >> n;
    m = n;
    for(int k=20;k>=0;k--) {
        x = (1 << k);
        if(n < x) continue;
        y = 4 * x - 2;
        l = n - x + 1;
        for(int i=0;i<l;i++) {
            a[x+i] = x-i-1;
            a[x-i-1] = x+i;
            r += y;
        }
        n -= 2 * l;
    }
    cout << r << endl;
    cout << a[0];
    for(int i=1;i<=m;i++) cout <<" "<< a[i];
    cout << endl;
    return 0;
}

 

posted @ 2013-04-05 07:07  aiiYuu  阅读(193)  评论(0)    收藏  举报