1121. Damn Single (25)

来源:互联网 发布:win8系统修复软件 编辑:程序博客网 时间:2024/05/22 18:39

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:
311111 2222233333 4444455555 66666755555 44444 10000 88888 22222 11111 23333
Sample Output:
510000 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考察挺多的




阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 陈嚣钟亦心 花秀神嚣 乡村大凶嚣龙傻子全文 乡村大凶嚣龙傻子2部 乡村大凶嚣孤城日落 天嚣阅读答案 天行加速嚣下载 360游览嚣 隗嚣 助听嚣 盛嚣尘上 嚣魏牟 机嚣人 嚣拼音 嚣尘上 嚣念什么 苏宁电嚣 嚣的读音 嚣的拼音 嚣臣 隗嚣的读音 乡村大凶嚣txt全本免费阅读 小村大凶嚣郑秀兰免费阅读 嚣张巫哲 嚣张歌词 嚣张狂妃 巫哲嚣张 嚣张巫哲txt 嚣张歌曲 嚣张简谱 嚣张小娇妻 嚣张txt 神医毒妃太嚣张 神医毒妃嚣张 逆天邪妃嚣张 逆天邪妃太嚣张 无双王妃太嚣张 冷王溺宠嚣张妃 我家邪妃太嚣张 妃常嚣张毒医大小组 绝色龙妃很嚣张