The problem appears to be very water - find brush

Report problem-solving 2019-8-8

1. Peas digital dedication and let him ease in the fields of science, but he also put into the frenzied little worried parents, in order to allow the children to fully develop, decided to broaden his field of study, just next to the house there is a painting training center Peas gave signed up to learn the first day of painting let Peas had a strong interest, also offered to buy a lot of brush, brush a variety of colors, Peas have a habit of the same color buy two brushes, a spare, and thus save the total brushes NN (NN is even and 1 <N <1061 <N <106). But the numbers of sensitive pervasive, Peas brain pop out of a strange question: if blindfolded and taken away any of a paintbrush, analysis of the remaining N-1N-1 brushes away to find out what colors you he can answer it?

Entry

The first line represents an integer number of brush remaining N-1N-1 is described in the title.
The second row N-1N-1 space-separated positive integers Ai (1≤Ai <231) Ai ( 1≤Ai <231), the remaining color number represents the brush.
Note: The data ensure a brush color number appears once, the rest have appeared twice.

Export

An integer line PP, color number represents take away the brush.

The company is said to be a face questions oh ......

First of all, you will certainly think of a way: we open a new array cnt. Each input a [i], cnt [a [i]] ++; to finally see who == 1;
Well, first congratulate you, think of a solution.
Secondly, I congratulate you, the company is dead and can not be you. . . You come up with such a spicy chicken method? ? Waiting for the overflow space bar. . .

In calm down I thought, we can first sort the array. Through the array for each element, looking down is absolutely no problem.
code show as below:

#include<bits/stdc++.h>
using namespace std;
int n,a[1000010];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i+=2)
        if(a[i]!=a[i+1]){
            printf("%d\n",a[i]);
            return 0;
        }
    return 0;
}

Congratulations again to you, expect to AC code, but the company still does not want you.

Because you show enough, not fast enough.

This approach is O (nlogn) slow death.
So, to teach you a sense Sao operation: bit computing.
You put an array or different each number up, it is the result of the output you want. (I bet you did not understand)
Let me explain: First, XOR (^) he is to turn into every digital binary for each binary digit:
0 = 0 ^ 00 ^ 1 = 1 ^ 0 ^ 0 = 0 1 1 = 1
, then, it is readily understood that a ^ a = 0. So, every other are the same or different up and set equal to what had happened. So, the end result is you ------ you're looking for a few! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Guess you like

Origin www.cnblogs.com/tushukai/p/11324218.html