Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
来源:互联网 发布:约瑟夫环问题 c语言 编辑:程序博客网 时间:2024/05/17 11:36
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.
34 74 77 4
0
54 77 42 119 71 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 999999999map<int,int> m1;//allmap<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;}
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- CodeForces 204B - Little Elephant and Cards
- codeforces 204B- Little Elephant and Cards
- Codeforces 204B Little Elephant and Cards
- Codeforces Round #129 (Div. 1) A Little Elephant and Interval
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- Codeforces Round #136 (Div. 2) B. Little Elephant and Numbers
- Codeforces Round #157 (Div. 1)B. Little Elephant and Elections 数位dp好
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array(简单莫队)
- Codeforces Round #129 (Div. 2)A. Little Elephant and Rozdil
- Codeforces Round #157 (Div. 1)---A. Little Elephant and Bits
- Codeforces Round #136 (Div. 2) / 221B Little Elephant and Numbers (数论)
- C++ const引用详解
- 1、shell指令操作【ubuntu】 - 常用shell命令
- HDU_3255 Farming 扫描线求面积并
- mysql 数据库管理中的安全问题(一)
- fatal error C1083: Cannot open include file: 'jpeglib.h ': No such file or
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Discuz X2版块分区后帖子列表左侧论坛导航只显示本区版块
- 反射
- 绘制表格
- NSGeometry.h 里面的方法详解
- 配置Apache+Tomcat集群
- DataGridView控件的使用
- 安装vs2008 x64位编译器:打开vs2008安装程序会出现加载组件的错误,
- 利用wifi连接android设备进行调试