HDU 5014 Number Sequence(西安网络赛H题)

HDU 5014 Number Sequence

题目链接

思路:对于0-n,尽量不让二进制中的1互相消掉就是最优的,那么只要两个数只要互补就可以了,这样每次从最大的数字,可以找到和他互补的数字,然后这个区间就能确定了,然后剩下的递归下去为一个子问题去解决

代码:

#include <cstdio>
#include <cstring>

const int N = 100005;

int n, a[N], ans[N];
int cnt[N];

int count(int x) {
	if (x == 0) return 1;
	int ans = 0;
	while (x) {
		ans++;
		x >>= 1;
	}
	return ans;
}

void dfs(int n) {
	if (n <= 0) return;
	int tmp = (n^cnt[n]);
	for (int i = n; i >= tmp; i--) {
		ans[i] = n + tmp - i;
	}
	dfs(tmp - 1);
}

int main() {
	for (int i = 0; i < N; i++)
		cnt[i] = ((1<<count(i)) - 1);
	while (~scanf("%d", &n)) {
		for (int i = 0; i <= n; i++)
			scanf("%d", &a[i]);
		memset(ans, 0, sizeof(ans));
		dfs(n);
		long long out = 0;
		for (int i = 0; i <= n; i++)
			out += (long long)(a[i]^ans[a[i]]);
		printf("%I64d\n", out);
		for (int i = 0; i <= n; i++)
			printf("%d%c", ans[a[i]], i == n ? '\n' : ' ');
	}
	return 0;
}
时间: 2024-07-28 17:04:19

HDU 5014 Number Sequence(西安网络赛H题)的相关文章

HDU 5011 Game(西安网络赛E题)

HDU 5011 Game 题目链接 思路:其实就求一个Nim和即可,要推也不难推,和为0下一个必然是胜态,因为至少取走一个,在怎么分也达不到原来那个值了,如果是非0值,就和原来Nim一样必然可以取一堆使得变成0 代码: #include <cstdio> #include <cstring> const int N = 100005; int n; long long a, sum; int main() { while (~scanf("%d", &

HDU 5017 Ellipsoid(西安网络赛K题)

HDU 5017 Ellipsoid 题目链接 思路:模拟退火大法好! 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int D[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; dou

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 5014 Number Sequence(贪心)

题目链接:hdu 5014 Number Sequence 题目大意:给定n,表示有0~n这n+1个数组成的序列a,要求构造一个序列b,同样是由0~n组成,要求∑ai⊕bi尽量大. 解题思路:贪心构造,对于n来说,找到n对应二进制的取反对应的数x,那么从x~n之间的数即可两两对应,然后x-1即是一个子问题. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 5014 Number Sequence ( 构造 )

HDU 5014 Number Sequence ( 构造 ) 题目意思: 给出一个数列 由0 - n 组成 然后需要构造一个数列 (也是由0-n组成),使得 sum(A[i] ^ B[i])的值最大. 分析: 异或不能出现比这个数大的情况,,所以要从大的数往前找. 现计算出当前判断数的二进制位的位数m,然后再与2^m - 1进行异或,这样会保证最大,具体为什么,自己可以想一想 比如: 5 - 101 -- 3位 - 对应 111 101 ^ 111 = 010 代码: #include <cs

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn&#39;t want to study

C*M....死于update的一个long long写成int了 心累 不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/31460 #include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn =

HDU 5014 Number Sequence(位运算)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少了一个操作,ans[i] = temp,却没有想到ans[temp] = i;所以就一直卡在这里了,因为我不确定这样是不是一一对应的,好吧,也没有想到这里,就差这么点了. 1 #include<cstdio> 2 #include<cstring> 3 #include<iost

hdu 5014 Number Sequence(西安网络赛1008)

Number Sequence                                                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 633    Accepted Submission(s): 300 Special Judge Problem Descrip