码迷,mamicode.com
首页 > 编程语言 > 详细

EZOJ #373排序

时间:2019-09-14 22:15:37      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:bit   代码   font   space   style   col   code   int   its   

分析

它居然真的是个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;
}

EZOJ #373排序

标签:bit   代码   font   space   style   col   code   int   its   

原文地址:https://www.cnblogs.com/yzxverygood/p/11520425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!