CF204B.Little Elephant and Cards

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Little Elephant loves to play with color cards.

He has n cards, each has exactly two colors (the color of the front side and the color of the back side). 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 that a set of cards on the table is funny if at least half of the cards have the same color (for 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.

小象喜欢玩彩色卡片。

他有 nn 张卡片,每张卡片恰好有两种颜色(正面颜色和背面颜色)。初始时,所有卡片均正面朝上平铺在桌面上。每次操作中,小象可以将任意一张卡片翻转至另一面。小象认为桌面上的一组卡片是“有趣的”,当且仅当至少一半的卡片具有相同的颜色(对每张卡片而言,只考虑其朝上的那一面的颜色)。

请帮助小象求出使这 nn 张卡片构成“有趣”集合所需的最少操作次数。

输入格式

The first line contains a single 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 are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card.

The numbers in the lines are separated by single spaces.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 卡片的数量。接下来的 nn 行描述所有卡片,每行描述一张卡片。每张卡片由一对不超过 10910^9 的正整数描述——即卡片两面的颜色。每行中的第一个数字表示卡片正面的颜色,第二个数字表示卡片背面的颜色。卡片正面的颜色可能与背面的颜色相同。

每行中的数字以单个空格分隔。

输出格式

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.

在单行中输出一个整数——所求的最小移动次数。如果无法使该集合变得“有趣”,则输出 -1。

输入输出样例

  • 输入#1

    3
    4 7
    4 7
    7 4

    输出#1

    0
  • 输入#2

    5
    4 7
    7 4
    2 11
    9 7
    1 1

    输出#2

    2

说明/提示

In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0.

In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.

在第一个样例中,初始时有三张卡片,颜色分别为 4、4、7。由于三张卡片中有两张颜色相同(均为 4),因此无需进行任何更改,答案为 0。

在第二个样例中,你可以翻转第一张和第四张卡片。翻转后,五张卡片中有三张的颜色为 7。

输入解题思路,AI测评打分。不知道怎么写?

首页