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(B1⊕B2,B2⊕B3,B3⊕B4,…,Bn−1⊕Bn,Bn⊕B1) 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
- 0≤Ai≤109,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; }