CF 177(div2) E(greedy )

来源:互联网 发布:it应届生简历模板 编辑:程序博客网 时间:2024/04/30 23:37
E. Polo the Penguin and XOR operation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little penguin Polo likes permutations. But most of all he likes permutations of integers from0 to n, inclusive.

For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number.

Expression means applying the operation of bitwise excluding "OR" to numbersx and y. This operation exists in all modern programming languages, for example, in languageC++ and Java it is represented as "^" and inPascal — as "xor".

Help him find among all permutations of integers from 0 ton the permutation with the maximum beauty.

Input

The single line contains a positive integer n (1 ≤ n ≤ 106).

Output

In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from0 to n with the beauty equal tom.

If there are several suitable permutations, you are allowed to print any of them.

Sample test(s)
Input
4
Output
 
因为亦或就是不进位的二进制加法,所以很容易想到尽量不要让每个位上的1浪费掉,就有了一个贪心的想法,从大到小,用全1的数去减当前的这个数,就是这个数应该对应的数,结合样例测试后的确是可行的。但是发现,对较小的数这样做,得到的对应数可能大于最大值,没法对每个数都用一个统一的方案解决,怎么办呢?其实可以发现,较小的数对应的数,在对称的那个较大数求较小数时应经就找到了,就是说我们不用一次次去找每个数对应的数,而是每次找一对数,这样问题就解决了。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <stack>#define LL long longusing namespace std;const int maxn = 1000000 + 5;LL pre[32];int ans[maxn];LL len(int x){    LL ret = 0;    while(x){        ret++;        x = x/2;;    }    return ret;}int main(){    int n;    LL total;    pre[0] = 0;    pre[1] = 1;    for(int i = 2;i < 32;i++) pre[i] = (pre[i-1]<<1) + 1;    while(cin >> n){        memset(ans,-1,sizeof(ans));        total = 0;        for(int i = n;i >= 0;i--){            if(ans[i] == -1){                int j = pre[len(i)]-i;                ans[i] = j;                ans[j] = i;                total += 2*pre[len(i)];            }        }        cout << total << endl;        for(int i = 0;i <= n;i++) printf("%d ",ans[i]);        cout << endl;    }    return 0;}

原创粉丝点击