Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
来源:互联网 发布:淘宝助理申通模板 编辑:程序博客网 时间:2024/05/14 12:45
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.
题目的大意是:给你n张卡片,每张卡片的正反面分别用一个数组代表一种颜色,刚开始时,卡片正面朝上,求最少的翻转次数使得卡片有一半以上是同一种颜色,如果不能实现,输出-1;
在这里要注意当卡片只有一张时的特殊情况!
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;struct cc{ LL d; int a;} p[200001];bool cmp(cc n,cc m) { return n.d<m.d; }int main(){ LL n,i,j,min,k,nn,sum,z=0; LL ss,ee; scanf("%lld",&n); j = 0; for(i=0;i<=n-1;i++) { scanf("%lld%lld",&ss,&ee); if(ss!=ee) { p[j].d=ss; p[j].a=1; j ++; p[j].d=ee; p[j].a=2; j ++; } else { p[j].d=ss; p[j].a=1; j ++; } } sum=j; if(n%2==0) nn=n/2; else nn=n/2+1; sort(p,p+sum,cmp); j=1; min=1e9+1; if(p[0].a==1) k=1; else k=0; for(i=1;i<=sum-1;i++) { if(p[i].d==p[i-1].d) { j ++; if(p[i].a==1) k++; if(k>=nn) { z=1; break; } if(j>=nn) { if(min>nn-k) min=nn-k; } } else { if(j>=nn) { if(min>nn-k) min=nn-k; } j=1; if(p[i].a==1) k=1; else k=0; } if(j+sum-1-i<nn) break; } if(z||n==1) printf("0\n"); else if(min==1e9+1) printf("-1\n"); else printf("%d\n",min); 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 (数论)
- HDU 3746 Cyclic Nacklace KMP
- swt转exe
- lua表的遍历-易错点
- 兼容sdk7&iOS7的issue解决小片段总结
- Pearson(皮尔逊)相关系数及MATLAB实现
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- 黑马程序员-第2天 JAVA 进制和运算符
- Ubuntu手动安装Flash Player
- 第五届蓝桥杯预赛练习——买不到的数字
- 如何判断两个字符串是否相互包含?
- php学习笔记(视频前50讲)
- [C#]数字颠倒输出;判断某天是一年中的第几天
- 1-1-日期结构体
- javascript:类,成员变量,成员函数,类变量,类方法,继承,多态