题目链接: 传送门

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的更多相关文章

  1. 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 ...

  2. Solution -「CF 1392H」ZS Shuffles Cards

    \(\mathcal{Description}\)   Link.   打乱的 \(n\) 张编号 \(1\sim n\) 的数字排和 \(m\) 张鬼牌.随机抽牌,若抽到数字,将数字加入集合 \(S ...

  3. 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)) 在 ...

  4. Codeforces Round #129 (Div. 2)

    A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...

  5. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

  6. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

  7. dp专题训练

    ****************************************************************************************** 动态规划 专题训练 ...

  8. 【DP专辑】ACM动态规划总结(转)

    http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强, ...

  9. dp有哪些种类

    dp有哪些种类 一.总结 一句话总结: 二.dp动态规划分类详解 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. * ...

随机推荐

  1. swift——uiwebview的使用

    首先,创建一个label: agreeDeal = UILabel() let tap = UITapGestureRecognizer.init(target: self, action: #sel ...

  2. ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。

    手机app去读写蓝牙设备....... 代码下载: 原文博客主提供Github代码连接,地址是:https://github.com/coolnameismy/demo ios连接外设的代码实现流程: ...

  3. lecture3-线性神经元和算法

    Hinton第三课 这节课主要是介绍NN的输出端常用的神经元,然后重点是说明怎么使用BP来计算偏导数,在Hinton这一课中,他提供了他1986年参与写的<并行分布处理>一书的第8章,49 ...

  4. 【python游戏编程之旅】第八篇---pygame游戏开发常用数据结构

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 上一个博客我们一起学习了pygame中冲突检测技术:http://www.cnblogs.com/msxh/ ...

  5. ASP.NET网站入侵第三波(fineui系统漏洞,可导致被拖库)

    注:屏蔽本漏洞的紧急通知:http://fineui.com/bbs/forum.php?mod=viewthread&tid=7863 本人小学文化,文采不好,写的不好请各位多多包含, 最近 ...

  6. Dapper Vs Dbentry

    公司项目数据库访问采用的dapper,以前没有用过.今天简单的测试下了,dapper和dbentry 查询效率情况. public ActionResult Test() { Sys_UserFaca ...

  7. java发送邮件

    1.需要用到javax.mail怎么下载呢?百度javax.mail就会看见http://www.oracle.com/technetwork/java/index-138643.html实际上这个项 ...

  8. 屠龙之路_大杀技之倚天屠龙_TenthDay

    惊天变! alhpa恶龙终于现身了!随之出现是屠龙天团的少年们多时不见的公主.alpha恶龙虽然元气大伤.意识不清,但是它庞大的身躯只要稍微动弹,足以重创在场的所有少年,以及现在还被恶龙牢牢囚在手心的 ...

  9. CSS巩固

    1. 浮动元素与非浮动元素在一行,浮动元素不占宽度.所以应将非浮动元素改为浮动,或让非浮动元素的宽度为当前行的宽度. 元素浮动之后,周围的元素会重新排列. 2. 布局找模板,或参考其他网站! 自己进行 ...

  10. PHP中生成json信息的方法

    <?php //php中生成json信息 //json_encode(数组/对象) $color = array('red','blue','green'); //[索引数组] echo jso ...