1121. Damn Single (25)

时间限制
300 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888

题意:输入n对夫妻的名单,再输入m个客人列表,判断今晚有多少人单身(已婚的,但是对方没来算单身)

分析:圣诞节做single dog的题真带感(雾

先记录下 夫妻的名单,输入客人时,若单身,则加入set中;若已婚并对象未出现,则加入set中;若已婚并对象已出现,则利用set的find,删除他们。

set的find还是很快的,20000个也不过14次查找。虽然暴力了一点,但实现很方便。

另外需要注意的是,因为00000也是客人的编号,所以夫妻名单要初始化成-1,用0的话会产生歧义(一夫多多多多妻)

代码:

#include<cstdio>
#include<set>
#include<cstring>
using namespace std;
int couple[100050];
set<int> single;
int main()
{int n,m,a,b;scanf("%d",&n);memset(couple,-1,sizeof(int)*100050);for(int i=0;i<n;i++){scanf("%d %d",&a,&b);couple[a]=b;couple[b]=a;}scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d",&a);if(couple[a]==-1) single.insert(a);else{if(single.find(couple[a])!=single.end())single.erase(single.find(couple[a]));else single.insert(a);}}printf("%d\n",single.size());set<int>::iterator it;for(it=single.begin();it!=single.end();it++)it==single.begin()?printf("%05d",*it):printf(" %05d",*it);
}

结尾:感觉pat里对set考察挺多的

1121. Damn Single (25)相关推荐

  1. 1121 Damn Single (25分)

    1121 Damn Single (25分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bein ...

  2. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

  3. 1121. Damn Single (25)-PAT甲级真题

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  4. 【PAT】1121. Damn Single (25)【哈希表】

    题目描述 "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are ...

  5. 1121 Damn Single (25 分)

    1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...

  6. PAT 甲级 1121  Damn Single

    1121 Damn Single (25 point(s)) "Damn Single (单身狗)" is the Chinese nickname for someone who ...

  7. PAT——1121 Damn Single 甲级

    1121 Damn Single 题目 题意 代码解析 AC代码 参考 题目 https://pintia.cn/problem-sets/994805342720868352/problems/99 ...

  8. PAT甲级1121 Damn Single :[C++题解]哈希表、结构体

    文章目录 题目分析 题目来源 题目分析 来源:acwing 思路:一对伴侣存入一个结构体中.查询的集合放入哈希表unordered_set<int> S;中.然后遍历每一对伴侣,如果同时存 ...

  9. 1121 Damn Single

    1121 Damn Single "Damn Single (单身狗)" is the Chinese nickname for someone who is being sing ...

最新文章

  1. 算法基础知识科普:8大搜索算法之二叉搜索树(中)
  2. mysql校对规则_MySQL中的校对规则
  3. 四年上册级计算机教学计划,小学数学四年级上册教学计划
  4. 深度学习浪潮下的自然语言处理,百度NeurIPS 2019展现领域新突破
  5. python PyQt5中文教程☞【第十节】PyQt5绘图(绘制文本drawText()、画点drawPoints()、设置颜色、QPen(画笔)绘制线条、QBrush(笔刷)绘制纹理
  6. 防火墙(13)——实现路由转发功能(1)
  7. vivado下创建基本时序周期约束
  8. 野生前端的数据结构练习(1)——栈
  9. [NEHE Couse] 05.3D Objects
  10. 基于统计的压缩算法:游程编码
  11. 自己编译nvm-window,解决无法修改镜像下载node很慢的问题!
  12. 全连接层后面加bn层_神经网络基本组成 - 池化层、Dropout层、BN层、全连接层 13...
  13. 计算机房通气换气次数,各种换气次数汇总
  14. 井字棋游戏的实现和注意细节
  15. android 有线网络,安卓手机免费“有线”上网
  16. oa系统服务器地址怎么查找,oa系统的服务器地址怎么看
  17. nginx配置文件及说明
  18. Spring - MySql实现高可用架构之MHA
  19. 低腰产品的软文营销之道 如何利用软文引流宣传产品利用软文营销提升低腰产品的知名度 如何打造有效的软文引流策略
  20. 爬虫工具之Beautiful Soup学习

热门文章

  1. 深度学习---从入门到放弃(九)RNN入门
  2. MIT 18.02 多变量微积分总结(Part II)
  3. In Search of an Understandable Consensus Algorithm(Extended Version)
  4. 糖价回暖,甩掉包袱的南宁糖业能跑多快?
  5. css如何导入特殊字体
  6. 页面显示LCD液晶字体,特殊字体,@font-face属性详细用法,配图
  7. xshow-1. 项目简介
  8. html天气插件iframe,HTML_利用iframe在网页中显示天气附效果截图,css: 复制代码代码如下: *{margi - phpStudy...
  9. 掌握成为优秀财务的核心能力
  10. 【对未来机器人的畅想】