CodeForces 204B - Little Elephant and Cards
来源:互联网 发布:windows7删除数据恢复 编辑:程序博客网 时间:2024/05/21 09:26
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 ofn cards funny.
The first line contains a single integer n(1 ≤ n ≤ 105) — the number of the cards. The followingn lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding109 — 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 color7.
=============================
给你一摞牌,两面有数字,问至少要翻转多少张牌可以使正面具有至少一半相同的数字
分别记录下正面和背面每个数出现的次数,算出是否符合条件即可。注意正面和背面可以相等,这时就不算背面,因为只需要保持它正面在上就行了
#include <iostream>#include <map>#include <cstdio>using namespace std;map<int,int>mp1,mp2;int a[111111],b[111111];int n;int MIN=1e9;int main(){ scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&a[i],&b[i]); if(a[i]==b[i]) { mp1[a[i]]++; } else { mp1[a[i]]++; mp2[b[i]]++; } } map<int,int>::iterator it; for(it=mp1.begin();it!=mp1.end();it++) { int t=it->first; if(mp1[t]>=(n+1)/2) { printf("0\n"); return 0; } if(mp1[t]+mp2[t]<(n+1)/2) continue; else { int temp=(n+1)/2-mp1[t]; MIN=min(temp,MIN); } } for(it=mp2.begin();it!=mp2.end();it++) { int t=it->first; if(mp2[t]>=(n+1)/2) { MIN=min((n+1)/2,MIN); break; } } if(MIN!=1e9) printf("%d\n",MIN); else printf("-1\n"); return 0;}
- CodeForces 204B - Little Elephant and Cards
- codeforces 204B- Little Elephant and Cards
- Codeforces 204B Little Elephant and Cards
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces 220B Little Elephant and Array
- CodeForces 220B Little Elephant and Array
- codeforces 221B Little Elephant and Numbers
- Codeforces-258B Little Elephant and Elections
- Codeforces 258B Little Elephant and Elections
- Codeforces 258B - Little Elephant and Elections
- codeforces 204A Little Elephant and Interval
- CodeForces 204A Little Elephant and Interval
- CodeForces 204A Little Elephant and Interval
- codeforces 204E Little Elephant and Strings
- CodeForces Round #136(220B) - Little Elephant and Array
- 线段树 CodeForces 220B - Little Elephant and Array
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- MATLAB R2012a与vs2010混合编程 error LNK2019
- AppIconSizes.com
- CMake的使用
- jxl导出图片
- 【站长笔记】Ubuntu下利用PureFTP搭建FTP服务器
- CodeForces 204B - Little Elephant and Cards
- *lds ----linux下的通用链接脚本
- Android使用自定义AlertDialog(退出提示框)
- java 第八天 面向对象(基础篇之练习题)
- shell中$(( )) 与 $( ) 还有${ }的区别
- GDB 进行调试 使用心得
- unity3d 脚本编译
- VMWare 三种工作模式(bridged、host-only、NAT)
- 画正方形