Codeforces 204B Little Elephant and Cards
来源:互联网 发布:湖北农村金融数据 编辑:程序博客网 时间:2024/05/22 13:05
题意:
给你n张卡片,每张卡片都有正反a,b两种颜色,问你至少需要反转多少次才能使正面的一样的颜色超过一半以上。
思路:
统计下每种颜色的个数,只有当一种颜色的个数>=(n+1)/2时我们才考虑它,然后判定下该颜色在正面的个数是否>=(n+1)/2如果大于不需要反转,如果小于则需要反转至少
(n+1)/2-在正面的个数,然后不断维护这个的最小值。
#include<cstdio>#include<map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int MAX=1e5+5;int n,a[MAX],b[MAX];map<int,int> sum,l;int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&a[i],&b[i]);sum[a[i]]++;if(a[i]!=b[i]) sum[b[i]]++;l[a[i]]++;}int ans=INF,Max=(n+1)/2;for(int i=1;i<=n;i++){if(sum[a[i]]>=Max){if(l[a[i]]>=Max){ans=0;break;}else ans=min(ans,Max-l[a[i]]);}if(sum[b[i]]>=Max){if(l[b[i]]>=Max){ans=0;break;}else ans=min(ans,Max-l[b[i]]);}}if(ans!=INF) printf("%d\n",ans);else printf("-1\n");return 0;}
0 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
- hdu 2579 Dating with girls(2)
- UISwitch控件-“应用小结”
- Find a way<hdoj2612>
- 2118数据结构实验之链表三:链表的逆置
- hadoop简单实例-WordCount
- Codeforces 204B Little Elephant and Cards
- JS中setTimeout()的用法详解
- java语言基础入门——异常处理
- 使用 Date 和 SimpleDateFormat 类表示时间
- UIScrollView的循环滚动+UIPageControll+NSTimer实现的自动循环滚动播放最终完善版
- 字符串匹配算法 之 (Horspool )Boyer-Moore-Horspool
- 复习java 总结的一些小细节(一)
- 顺序表应用6:有序顺序表查询
- 趣味100道C语言编程题