HDU5014 2014ACM-ICPC 亞洲區域賽西安賽區網路賽H題 Number Sequence
阿新 • • 發佈:2019-02-13
Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1885 Accepted Submission(s): 561 Special JudgeProblem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai
Input There are multiple test cases. Please process till EOF. For each case, the first line contains an integer n(1 ≤ n ≤ 105
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
這題在之前做時候完全想不到思路,直到比賽2小時腦子裡突然閃現出一個YY思路,那就是如果對於一個0-n的序列,能構成的最大異或序列,需要這樣構想,對於n,能構成的最大數字需要與n取反配對(n與n取反異或運算後所有二進位制位置均為1,即最大),我們設n取反為m,那麼n-1應當與m+1配對,依次類推。這樣,我們得到了m-n所有的配對方式。然後,不斷迴圈配對m-1與m-1取反區間內的數字,直到配對至0則完成全部配對,此時即為異或配對的最大值。。。輸出即為答案。。。還是挺簡單的嘛。。。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
__int64 a[100005],pa[100005];
__int64 cal(__int64 pa)
{
__int64 ans=0,n=0;
while(pa>0)
{
a[n]=pa%2;
n++;
pa=pa/2;
}
__int64 tap=1;
for(int j=0;j<n;j++)
{
if(a[j]==0)
ans+=tap;
tap=tap*2;
}
return ans;
}
int main()
{
// freopen("in.txt","r",stdin);
int n;
while(cin>>n)
{
for(int i=0;i<=n;i++)
scanf("%d",&pa[i]);
__int64 pat=cal(n),sum=0,psum=0;
sum+=(n-pat)*(pat+n+1);
for(int i=pat,j=n;i<=n;i++)
{
a[i]=j;
psum+=(i^j);
j--;
}
pat--;
while(pat>0)
{
__int64 item=cal(pat);
for(int i=item,j=pat;i<=pat;i++)
{
a[i]=j;
psum+=(i^j);
j--;
}
sum+=(pat-item)*(pat+item+1);
pat=item;
pat--;
}
cout<<psum<<endl;
for(int i=0;i<n;i++)
printf("%d ",a[pa[i]]);
printf("%d\n",a[pa[n]]);
}
return 0;
}