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.
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.
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.
3 4 7 4 7 7 4
0
5 4 7 7 4 2 11 9 7 1 1
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.
水题……感觉这题应该放在A题的位置的……
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define INF 999999999
map<int,int> m1;//all
map<int,int> m2;//upper side...
int main()
{
int i,j,n,x,y,ans;
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
m1[x]++;
if (x!=y) m1[y]++;
m2[x]++;
}
map<int,int>::iterator it;
ans=INF;
for (it=m1.begin();it!=m1.end();++it)
{
if (it->second>=(n+1)/2)
{
ans=min(ans,max((n+1)/2-m2[it->first],0));
}
}
printf("%d\n",ans==INF?-1:ans);
return 0;
}
本文介绍了一个简单的编程问题,目标是帮助一只小象通过翻转卡片使至少一半的卡片颜色相同,以此来找到最少的操作次数。文章提供了完整的代码实现,并解释了输入输出的要求。

被折叠的 条评论
为什么被折叠?



