“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。
输入格式:
输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(≤ 10 000),为参加派对的总人数;随后一行给出这 M 位客人的 ID,以空格分隔。题目保证无人重婚或脚踩两条船。
输出格式:
首先第一行输出落单客人的总人数;随后第二行按 ID 递增顺序列出落单的客人。ID 间用 1 个空格分隔,行的首尾不得有多余空格。
输入样例:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
输出样例:
5
10000 23333 44444 55555 88888
思路:
将所有的couple存储在结构体中,然后输入所有的客人名单时,将出席的客人存储到map中(map的key值为客人编号,value置为一),处理好输入信息之后,设置一层for循环,以couple数作为循环的次数, 若是此对couple都在map中出现,则将对于客人编号在map中的value置为零,最后循环输出map中value值为1 的客人编号即可。(由于map会对存储进的数自动排序,所以无需再进行排序处理)
代码实现:
#include <bits/stdc++.h>
using namespace std;
typedef struct node{
int a,b;
}Couple;
int main() {
int N,M,t,sum = 0;
map<int,int> temp;
map<int,int>::iterator it;
scanf("%d",&N);
Couple couple[N];
//将所有的couple存储下来
for(int i = 0;i < N;i++){
scanf("%d%d",&couple[i].a,&couple[i].b);
}
scanf("%d",&M);
//将出席的客人编号存储到map中,并将其value值置为一
for(int i = 0;i < M;i++){
scanf("%d",&t);
temp[t] = 1;
}
for(int i = 0;i < N;i++){
//若某对couple的编号都在map中出现,说明其二人并不落单,相应的在map中的value值置0
if(temp.find(couple[i].a) != temp.end()&&temp.find(couple[i].b) != temp.end()){
temp[couple[i].a] = 0;
temp[couple[i].b] = 0;
sum++;
}
}
printf("%d\n",M-sum*2);
//循环输出即可
if(M-sum*2 != 0){
int num = 0;
for(it = temp.begin();it != temp.end();it++){
if(it->second == 1){
num++;
if(num == 1)
printf("%05d",it->first);//注意输出格式,必须为5位,不足五位数补0
else
printf(" %05d",it->first);
}
}
printf("\n");
}
return 0;
}