Practice Topic 4 Little Elephant and Cards

Source: Internet
Author: User

Little Elephant and CardsTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i64u

Description

The Little Elephant loves to play with color cards.

He has n cards, each with exactly and colors (the color of the front side and the color of the back Si DE). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks a set of cards on the table is funny if at least half of the cards has the same color (fo R each card the color of the upper side is considered).

Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.

Input

The first line contains a integer  N (1≤ n ≤105)  -the number of the cards. The Following  n  lines contain the description of all cards, one card per line. The cards is described by a pair of positive integers not exceeding 109 -colors of both sides. The first number in a line was the color of the front of the card, the second one-of the back. The color of the front of the coincide with the color of the card.

The numbers in the lines is separated by a single spaces.

Output

On a single line print a single integer-the sought minimum number of moves. If It is impossible to make the set funny, print-1.

Sample Input

Input



7 4
Output
0
Input





1 1
Output
2
Topic Meaning:
Remake of the game, the front and back there are 1e9 colors, when the same color facing on the number greater than (n + 1)/2 large, is funny
Seek the remake of the Son at least several times, into funny.
Thinking Analysis:
With the normal thinking has been timed out, alas, to see others understand.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;structnode{intx; intM//record front and rear sides}s[201000];BOOLCMP (node A,node b) {returnA.x <b.x;}intMain () {intN;  while(Cin >>N) {intK =0; intA, B;  for(inti =0; I < n; i++) {scanf ("%d%d", &a, &b); if(A! =b) {s[k].x=A; S[k++].M =0; s[k].x=b; S[k++].M =1; }            Else{s[k].x=A; S[k++].M =0; }} sort (s, S+ K, CMP);//from small to large        intL =-1, ans =0; intI, j =0;  for(i =0; I < K; i =j) {if(S[I].M)//judging the positive and negativeAns =0;//Reverse            Elseans=1;//is             for(j = i +1; J < k && s[i].x = = s[j].x; J + +)               if(S[J].M = =0)//Judging how many of the faces are the sameans++; if(J-i >= (n +1) /2)            {                if(Ans >= (n +1) /2) {L=0; I=K; J=K;  Break; }                Else                {                    if(L = =-1) L= (n +1) /2-ans; ElseL= Min (L, (n +1) /2-ans); }}} printf ("%d\n", L); }    return 0;}/*use a struct array, record the color of the front and back sides of each card, and record whether they are on the front or back, then sort the color by using sort, then traverse all the colors, and when a color appears >=n/2, find the color that satisfies the situation, and so on, find    The smallest case can be. Special Note: Some cards have the same positive and negative color, so just record their front. Also, when a color is in the positive number of times >=N/2, the direct output of 0 can be. */

Practice Topic 4 Little Elephant and Cards

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.