CodeForces 288C - Polo the Penguin and XOR operation(思维)

题意:

就是让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为   SUM  =   a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n

构造出一个序列使得和最大

题解:

策略为使得每次异或出来的结果的1尽可能多,而优先从最大的n  开始考虑,因为n  最有可能出更大的数字

代码:

#include<stdio.h>
#include<string.h>
int Ans[1000005];
int main()
{
    int n, x;
    while(scanf("%d", &n) != EOF)
    {
         x = 1;

         while(x <= n)  x = x<<1;
         x --;
         memset(Ans, 255, sizeof(Ans));
         for(int i = n; i>= 0; i--)
         {
             if(Ans[i] != -1) continue;
          //   printf("%d\n", x);
             while((x^i) > n || Ans[x^i] != -1)  x = x>>1;
        //     printf("%d %d\n", i, x);
             Ans[x^i] = i, Ans[i] = x^i;
         }
         __int64 sum = 0;
         for(int i = 0; i <= n; i++)
         {
             sum += (i^Ans[i]);
         }
         printf("%I64d\n%d", sum, Ans[0]);
         for(int i = 1; i <= n;  i++)
         printf(" %d", Ans[i]);
         printf("\n");
    }
}
时间: 2024-08-07 22:58:21

CodeForces 288C - Polo the Penguin and XOR operation(思维)的相关文章

CodeForces 288C - Polo the Penguin and XOR operation(思路)

题意:给定一个 n (1 <= n <= 10^6),求(0 ^ p0) + (1 ^ p1) + (2 ^ p2) +-- + (n ^ pn) 的最大值,其中p0 ~ pn为 0 ~ n 中的数,且每个数只利用一次. 从n ~ 0枚举所有的数,对于每个数找它异或后得到二进制都是 1 的数或者 1 尽量多的数. 对于每个数,先找到与它二进制位数相同的全是 1 的数,然后把这个当成结果,与目前的数异或得到另一个数并记录,然后同时记录另一个数(可能用不到,但是为了节省时间). 加起来的结果可能超

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)

Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p?=?p0,?p1,?-,?pn, Polo has defined its beauty - number . Expression means applying the operation of bitwise excluding "

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

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配. #include<cstdio> #include<cmath> #include<cstring> #include<iostream>

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation

题意:让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为 SUM = a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n 感想 :之前没做过有关位运算的题,对这一块很陌生,两个数异或以后,如果二进制每一位都为1,那么一定最大,找规律发现当n为偶数时 除开0以外,其他的都是成对出现 当n为奇数时 都是成对出现 例如n=4, 0 1 2 3 4 分别对应0 2 1 4 3 :n=5时 0 1 2 3 4 5 分别对应1 0 5

codeforces 289B - Polo the Penguin and Matrix 二分+dp

题意:给你一个序列,每一次可以对序列里面任意数+d 或者 -d 问你最少多少步能够使得数列里面所有的数相等 解题思路:从 1 - 10000 枚举这个数,二分找数列中小于等于它的最大的那个数,然后求前缀和以后刻意快速求出差值和的绝对值,差值和/d 就是我们所求数. 解题代码: 1 // File Name: 289b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月29日 星期二 22时33分11秒 4 5 #include<vecto

Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)

Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1?≤?pi?≤?n). Little penguin Polo loves walking ar

codeforces 288A:Polo the Penguin and Strings

Description Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the string's length eq

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

CodeForces 276D – Little Girl and Maximum XOR 贪心

整整10个月后第二次搞这个问题才搞懂........第一次还是太随意了. 解题思路: 经过打表可得规律答案要么是0 要么是2的N次 - 1 要得到最大的XOR值,其值一定是2的N次 - 1 即在 l 和 r 的二进制中,从左到右遍历过去,如果碰到 (2 ^ i) & l 为 1 , (2 ^ i) & r 为 0 即在 l 和 r 之间一定存在 形如 10+ 和01+这样的数. 则可说明在[l , r]中存在 1000000000 和 0111111111 可得到最大XOR值为2的N次 -