EZOJ #373排序

分析

它居然真的是个nlog^3暴力?!

两个数在加小于min(lowbit(x),lowbit(y))的数时对他们的奇偶性不影响

因此每次加上min(lowbit(x),lowbit(y))判断此时的奇偶性

直接排序即可

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long 
int a[100100],n;
inline int lb(int x){return x&(-x);}
inline int cnt(int x){int res=0;while(x)res+=(x&1),x>>=1;return res;}
inline bool cmp(int x,int y){while((cnt(x)&1)==(cnt(y)&1)){int t=min(lb(x),lb(y));x+=t,y+=t;}return !(cnt(x)&1);}
signed main(){
    int i,j,k;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)scanf("%lld",&a[i]);
    sort(a+1,a+n+1,cmp);
    for(i=1;i<=n;i++)printf("%lld ",a[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yzxverygood/p/11520425.html