Manipulative Numbers

Problem Statement

Suppose that A is a list of n numbers {A1,A2,A3,,An} and B={B1,B2,B3,..,Bn} is a permutation of these numbers, we say B is K-Manipulative if and only if:

M(B)=minimum(B1B2,B2B3,B3B4,,Bn1Bn,BnB1) is not less than 2K , where represents the XOR operator.

You are given A . Find the largest K such that there exists a K-manipulative permutation B .

Input:

The first line is an integer N . The second line contains N space separated integers - A1 A2  An .

Output:
The largest possible K , or 1 if there is no solution.

Constraints:

  • 1<n<=100
  • 0Ai109,where i[1,n]

Sample Input #00

3  
13 3 10

Sample Output #00

2

解题思路:Bi^Bi+1 >= 2^K当且仅当(Bi>>K) != (Bi+1>>K),对于上述的排列如果M(B)=K,则Bi>>K相同的数最多不能超过的n/2,这个分为奇数和偶数两种情况,画画图就很明显了。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
map<int, int> myMap;
int arr[110];

int main() {
    
    int n;
    int ans = -1;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &arr[i]);
    }
    for(int k = 30; k >= 0; --k) {
        myMap.clear();
        for(int i = 1; i <= n; ++i) {
            myMap[arr[i]>>k]++;
        }
        bool ok = true;
        for(map<int,int>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter) {
            if(iter->second > n/2) {
                ok = false;
                break;
            }
        }
        if(ok) {
            ans = k;
            break;
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值