HihoCoder#1509 : 异或排序(二进制)

题意

题目链接

Sol

挺简单的吧。考虑两个元素什么时候不满足条件

\(a_i\)\(a_i + 1\)最高的不同位分别为0 1,显然\(S\)的这一位必须为\(0\),否则这一位必须为\(1\)

剩下的就没有限制条件了

时间复杂度:\(nlogn\)??????!!!!!!

#include<bits/stdc++.h>
#define LL long long
#define int long long  
using namespace std;
const int MAXN = 62, B = 60;
inline int read() {
    int x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N;
LL a[MAXN], mark[MAXN];
main() {
    N = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    memset(mark, -1, sizeof(mark));
    LL ans = 1ll << 60;
    for(int i = 1; i <= N - 1; i++) {
        for(int x = B; x >= 0; x--) {
            int aa = (a[i] >> x) & 1, bb = (a[i + 1] >> x) & 1;
            if(aa != bb) {
                int now = aa < bb ? 1 : 2;
                if((mark[x] != now) && (mark[x] != -1)) {puts("0"); exit(0);}
                if(mark[x] == -1) ans /= 2;
                mark[x] = now; 
                break;
            }
        }       
    }
    cout << ans;
}   
/*
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值