HDU 5014解题报告
2014-11-25 17:25
399 查看
Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536K (Java/Others)
Total Submission(s): 1279 Accepted Submission(s): 548
Special Judge
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 sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)
(sequence B should also satisfy the rules described above)
Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.
Input
There are multiple test cases. Please process till EOF.
For each case, the first line contains an integer n(1 ≤ n ≤ 105), The second line contains a0,a1,a2,...,an.
Output
For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b0,b1,b2,...,bn. There is exactly one space between bi and bi+1(0
≤ i ≤ n - 1). Don’t ouput any spaces after bn.
Sample Input
4 2 0 1 4 3
Sample Output
20 1 0 2 3 4
Source
2014 ACM/ICPC Asia Regional Xi'an Online
Recommend
hujie | We have carefully selected several similar problems for you: 5111 5110 5109 5108 5107
解题思路
这道题关键是在a[i]的范围是在0~n之间,而要保证a[0]^b[0]+a[1]^b[1]+……a
^b
的值最大。也就是要尽量将a[i]在二进制下的每一位取反。但是这样容易产生重复的问题,不知道如何去取舍。比如样例中的a[i]为2时,b[i]可以取1,a[i]为0时,b[i]可以取1。这样便产生了问题。。所以为了保证数值的最大,2^1=3,而0^1=1,所以应该优先满足较大的数。又因为a[i]和b[i]的范围都是0~n。所以可以从n到0做一个循环,从大到小逐个数进行考虑。将这个i的每一位取反,得到一个新的数。然后就得到一对数,保证他们局部的异或值最大。然后将这些异或的值加起来即可。
总的来说,这道题用到了贪心的思想,而且用到了哈希的思想,建立一一映射。还是比较巧妙的。
参考代码
#include<cstdio> #include<iostream> #define __CLR(x) memset(x,-1,sizeof(x)) #define ll long long using namespace std; const int maxn=100010; int a[maxn],b[maxn]; int main() { //ios_base::sync_with_stdio(0); int n; while(~scanf("%d",&n)) { for(int i=0; i<=n; i++) scanf("%d",&a[i]); ll ans=0; __CLR(b); for(int i=n; i>=0; i--) { if(b[i]>=0) continue; int num=0,k=i,s=1; while(k>0) { int t=(k%2)?0:1; num=num+s*t; s*=2; k/=2; } b[num]=i; b[i]=num; ans+=2*(b[i]^b[num]); } printf("%I64d\n",ans); for(int i=0;i<=n;i++) { if(i>0) printf(" "); printf("%d",b[a[i]]); } printf("\n"); } }
相关文章推荐
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
- Hdu 1084 What Is Your Grade?解题报告
- HDU--2104--hide handkerchief--解题报告(辗转相除的运用)
- hdu 1064 解题报告
- HDU 2680 Choose the best route 解题报告
- HDU 3732 Ahui Writes Word 解题报告
- hdu 1102 pku 2421 解题报告
- HDU 1870 愚人节的礼物 解题报告
- HDU 2469 Fire-control System解题报告
- hdu 2139解题报告
- HDU 1231 畅通工程 解题报告
- HDU第11版解题报告(农夫版)
- hdu 2516解题报告
- HDU 3335 解题报告
- hdu 1019解题报告
- Hdu 1009 FatMouse' Trade解题报告
- HDU 3336 解题报告
- 终曲(hdu 2572)解题报告
- HDU 1158 Emloyment Planning 解题报告
- HDU 1856 More is better 解题报告