[Offer收割]编程练习赛12 register

Ended

Participants:1323

Verdict:Accepted
Score:100 / 100
Submitted:2017-04-02 14:12:13

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    long long int solve(vector<int> &nums) {
        long long int maxm = 0;
        int mask = 0;
        for(int i = 20; i >= 0; -- i) {
            mask |= (1 << i);
            map<int, int> prefix;
            for(auto n : nums) {
                ++ prefix[n & mask];
            }
            if(prefix.size() == 1) { // all match
                continue;
            }
            auto smallit = prefix.begin();
            auto largeit = prefix.rbegin();
            if(largeit->second > 1) {
                vector<int> tmp;
                for(auto n : nums) {
                    if((n & mask) == largeit->first) {
                        tmp.push_back(n);
                    }
                }
                swap(nums, tmp);
                continue;
            }
            else {
                auto it = find_if(begin(nums), end(nums), [=](const int n) { return (n & mask) == largeit->first; });
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX