CF 204B Little Elephant and Cards
题目链接: 传送门
Little Elephant and Cards
time limit per test:2 second memory limit per test:256 megabytes
Description
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.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — 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 10^9 — 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.
Output
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.
Sample Input
3
4 7
4 7
7 4
5
4 7
7 4
2 11
9 7
1 1
Sample Output
0
2
解题思路:
题目大意:有N张牌,每张牌正反两面都有颜色,问最少要翻转几次才能才能使一半的卡有相同的颜色。
感觉是STL的应用,选好map来找键值,set来创建集合,题目就很简单了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef __int64 LL;
int main()
{
int N;
while (~scanf("%d",&N))
{
LL x,y;
map<LL,LL>m1,m2;
map<LL,LL>::iterator itt;
set<LL>s;
set<LL>::iterator it;
for (int i = 0;i < N;i++)
{
scanf("%I64d%I64d",&x,&y);
m1[x]++;
if (x != y)
{
m2[y]++;
}
s.insert(x);
s.insert(y);
}
LL res = INF;
LL tmp = (N+1)/2;
for (it = s.begin();it != s.end();it++)
{
x = *it;
if (m1[x] + m2[x] >= tmp)
{
res = min(res,max(0LL,tmp - m1[x]));
}
}
if(res == INF)
{
res = -1;
}
printf("%I64d\n",res);
}
return 0;
}
CF 204B Little Elephant and Cards的更多相关文章
- CF 258B Little Elephant and Elections [dp+组合]
给出1,2,3...m 任取7个互不同样的数a1,a2,a3,a4,a5,a6,a7 一个数的幸运度是数位上4或7的个数 比方244.470幸运度是2. 44434,7276727.4747,7474 ...
- Solution -「CF 1392H」ZS Shuffles Cards
\(\mathcal{Description}\) Link. 打乱的 \(n\) 张编号 \(1\sim n\) 的数字排和 \(m\) 张鬼牌.随机抽牌,若抽到数字,将数字加入集合 \(S ...
- Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)
vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...
- Codeforces Round #129 (Div. 2)
A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
- 【DP专辑】ACM动态规划总结
转载请注明出处,谢谢. http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 ...
- dp专题训练
****************************************************************************************** 动态规划 专题训练 ...
- 【DP专辑】ACM动态规划总结(转)
http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强, ...
- dp有哪些种类
dp有哪些种类 一.总结 一句话总结: 二.dp动态规划分类详解 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. * ...
随机推荐
- 虚拟机开机提示Operating System not found解决办法
为了更好体验windows更多操作系统,有些用户会在VMware虚拟机中安装XP.win7或win8等等系统,有用户反映在虚拟机中安装XP开机后提示"Operating System not ...
- 使用markdown编辑evernote(印象笔记)的常用方法汇总
原文发表在我的博客主页,转载请注明出处 前言 正所谓工欲善其事,必先利其器,本文将要介绍的evernote和markdown都是程序员必备的工具 虽然国内现在有了很多evernote的替代品,做的比较 ...
- Bootstrap系列 -- 3. 段落
一. 段落基本用法 1. 段落使用<p>标签 2. 段落全局使用font-size=14px字体 ..... 更多请使用Firefox 查看 <p> 华盛顿大学和清华大学共同在 ...
- Linux企业集群用商用硬件和免费软件构建高可用集群PDF
Linux企业集群:用商用硬件和免费软件构建高可用集群 目录: 译者序致谢前言绪论第一部分 集群资源 第1章 启动服务 第2章 处理数据包 第3章 编译内容 第二部分 高可用性 第4章 使用rsync ...
- 转 Linux日志文件系统及性能分析
日志文件系统可以在系统发生断电或者其它系统故障时保证整体数据的完整性,Linux是目前支持日志文件系统最多的操作系统之一,本文重点研究了Linux常用的日志文件系统:EXT3.ReiserFS.XFS ...
- JVM垃圾收集器介绍
垃圾回收算法是GC的方法论,垃圾收集器就是内存回收的具体实现. 一.Serial 收集器 单线程收集器,在进行GC时,必须暂停所有的工作线程(Stop The World),直到GC收集结束. 缺点: ...
- ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案
摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...
- 线段树 poj3225
U:把区间[l,r]覆盖成1I:把[-∞,l)(r,∞]覆盖成0 D:把区间[l,r]覆盖成0C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换S:[l,r]区间0/1互换 因 ...
- hdu2665 && poj2104划分树
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 47066 Accepted: 15743 Ca ...
- SurfaceView, TextureView, SurfaceTexture等的区别
SurfaceView, GLSurfaceView, SurfaceTexture以及TextureView是Android当中名字比较绕,关系又比较密切的几个类.本文基于Android 5.0(L ...