hdu 5014 Number Sequence(西安网络赛1008)
2014-09-15 23:04
316 查看
Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536K (Java/Others)
Total Submission(s): 633 Accepted Submission(s): 300
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
从大往小的找,知道发现n+j的值是二进制全为1的数,则会发现这一段(n-1)+(j+1)的值都为二进制全为1的数,这一段就处理好了,接着再往前处理。
代码:
//390ms #include <iostream> #include <cstdio> #include <cstring> const int maxn=100000+1000; using namespace std; int hash[20]; int a[maxn]; int b[maxn]; int main() { int n; hash[0]=0; hash[1]=1; for(int i=1;i<19;i++) { hash[i+1]=(hash[i]<<1)+1; } while(~scanf("%d",&n)) { for(int i=0;i<=n;i++) scanf("%d",&a[i]); int temp; int sign=0; int m=n; while(m>=0) { if(m==0)//特判0 { b[0]=0; break; } for(int i=0;i<=19;i++)//找到第一个各位全为1的且比m大 { if(m<=hash[i]) { temp=hash[i]; break; } } for(int i=m-1;i>=0;i--) { if(m+i==temp)//找到左边对称的点 { sign=i; break; } } for(int i=sign;i<=m;i++) { b[i]=temp-i; } m=sign-1; } long long ans=0; for(int i=0;i<=n;i++) ans=ans+(i^b[i]); printf("%I64d\n",ans); for(int i=0;i<n;i++) printf("%d ",b[a[i]]); printf("%d\n",b[a ]); } return 0; }
相关文章推荐
- hdu 5014 Number Sequence 西安网络赛
- HDU 5014 Number Sequence(西安网络赛H题)
- hdu 5014(贪心+异或 西安网络赛)
- 2014西安赛区网络赛 5014 Number Sequence
- HDU 5014 Number Sequence(位运算)
- HDU-#5014 Number Sequence(贪心+构造)
- hdu 2014西安赛区网络赛 5011Game
- 【瞎搞】HDU 5014 Number Sequence 构造
- HDU 5009 Paint Pearls(西安网络赛C题)
- hdu5014——Number Sequence
- HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)
- hdu 4766 Network 长春网络赛 1008 计算几何
- hdu 5014 number sequence 贪心
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)
- HDU 5015 233 Matrix(西安网络赛I题)
- HDU 5012 Dice(西安网络赛F题)
- HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi'an Online) 题解
- HDU 5007 Post Robot(西安网络赛A题)
- HDU 5014 Number Sequence(分类模拟)
- HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi'an Online