CodeForces 288C Polo the Penguin and XOR operation
2016-07-28 09:44
489 查看
题意:给一个数 n,求0~n的一个排列,使得这个排列与0-n的对应异或之和最大。
分析:两个数二进制数正好互补,异或就是最大的,比如,一个数是100,那么我们要找11,(都是二进制)
就是这样找,而且两两正好配对,如果多了一个就是0。怎么找那另一个和它互补的数呢?用11...1 - 本身
借鉴了MZH的代码
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
int dx[]= {0,1,1,1,0,-1,-1,-1};
int dy[]= {1,1,0,-1,-1,-1,0,1};
int gcd (const LL &a, const LL &b) {return b==0?a:gcd (b,a%b);}
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int bitcnt (int n) {
int cnt=0;
while (n) {
n=n>>1; cnt++;
}
return cnt;
}
map<LL,LL>pos;
int main() {
memset (a,0,sizeof (a) );
int n; scanf ("%d",&n);
for (int i=n; i>=0; --i) {
int len=bitcnt (i);//获取位数
LL maxv= (1<<len)-1;//构造全1的那个二进制数
if (!pos.count (i) &&!pos.count (maxv^i) ) {
pos[i]=maxv^i;
pos[maxv^i]=i;//两两配到一起
}
}
LL sum=0;
for (int i=0; i<=n; ++i) {sum+=pos[i]^i;}//求和Σi^A[i]
printf ("%I64d\n",sum);
for (int i=0; i<=n; ++i) {
printf ( (i) ?" %I64d":"%I64d",pos[i]);
}
return 0;
}
题意:给一个数 n,求0~n的一个排列,使得这个排列与0-n的对应异或之和最大。
分析:两个数二进制数正好互补,异或就是最大的,比如,一个数是100,那么我们要找11,(都是二进制)
就是这样找,而且两两正好配对,如果多了一个就是0。怎么找那另一个和它互补的数呢?用11...1 - 本身
借鉴了MZH的代码
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
int dx[]= {0,1,1,1,0,-1,-1,-1};
int dy[]= {1,1,0,-1,-1,-1,0,1};
int gcd (const LL &a, const LL &b) {return b==0?a:gcd (b,a%b);}
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int bitcnt (int n) {
int cnt=0;
while (n) {
n=n>>1; cnt++;
}
return cnt;
}
map<LL,LL>pos;
int main() {
memset (a,0,sizeof (a) );
int n; scanf ("%d",&n);
for (int i=n; i>=0; --i) {
int len=bitcnt (i);//获取位数
LL maxv= (1<<len)-1;//构造全1的那个二进制数
if (!pos.count (i) &&!pos.count (maxv^i) ) {
pos[i]=maxv^i;
pos[maxv^i]=i;//两两配到一起
}
}
LL sum=0;
for (int i=0; i<=n; ++i) {sum+=pos[i]^i;}//求和Σi^A[i]
printf ("%I64d\n",sum);
for (int i=0; i<=n; ++i) {
printf ( (i) ?" %I64d":"%I64d",pos[i]);
}
return 0;
}
相关文章推荐
- Codeforces 612C: Replace To Make Regular Bracket Sequence(栈)
- HDU-1806 Frequent values (RMQ, 最大连续值区间)
- POJ 2371 Questions and answers
- HDOJ 4027 Can you answer these queries?(线段树+区间标记)
- Stacked Attention Networks for Image Question Answering
- request.getParameterNames()步骤
- Longest Ordered Subsequence
- Burp Suite
- Longest Increasing Subsequence
- iOS开发从入门到精通--UIGesture手势扩展,平移,滑动,长按
- Java中ProcessBuilder使用
- what does “static int function(…) __acquires(..) __releases(…){” mean?
- [转]Quick Tip: Error: 18456, Severity: 14, State: 38.
- POJ-2533-Longest Ordered Subsequence
- Java中String、StringBuffer和StringBuilder
- 86.View the Exhibit to examine the output for the CROSSCHECK BACKUP command. Which statement is true
- vue.js表单数据双向绑定
- ProgressBar(进度条)
- HDU 3415 Max Sum of Max-K-sub-sequence(长度不超过k的最大连续子序列和,单调队列)
- android开发仿ios的UIScrollView