您的位置:首页 > 其它

Codeforces Round #129 (Div. 1)B Little Elephant and Cards

2016-07-29 22:15 399 查看

题目链接: 传送门

Little Elephant and Cards

time limit per test:2 second     memory limit per test:256 megabytes

Description

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.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5) — 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 10^9 — 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.

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

3
4 7
4 7
7 4

5
4 7
7 4
2 11
9 7
1 1

Sample Output

0

2

解题思路:

题目大意:有N张牌,每张牌正反两面都有颜色,问最少要翻转几次才能才能使一半的卡有相同的颜色。
感觉是STL的应用,选好map来找键值,set来创建集合,题目就很简单了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef __int64 LL;

int main()
{
int N;
while (~scanf("%d",&N))
{
LL x,y;
map<LL,LL>m1,m2;
map<LL,LL>::iterator itt;
set<LL>s;
set<LL>::iterator it;
for (int i = 0;i < N;i++)
{
scanf("%I64d%I64d",&x,&y);
m1[x]++;
if (x != y)
{
m2[y]++;
}
s.insert(x);
s.insert(y);
}
LL res = INF;
LL tmp = (N+1)/2;
for (it = s.begin();it != s.end();it++)
{
x = *it;
if (m1[x] + m2[x] >= tmp)
{
res = min(res,max(0LL,tmp - m1[x]));
}
}
if(res == INF)
{
res = -1;
}
printf("%I64d\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: