1065 單身狗(25 分)
“單身狗”是中文對于單身人士的一種愛稱。本題請你從上萬人的大型派對中找出落單的客人,以便給予特殊關愛。
輸入格式:
輸入第一行給出一個正整數 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
分析:用map儲存所有人的配偶和賓客到場情況,之後檢測所有人的配偶是否到場(若不存在則視為未到場),如為未到場則送入set中。最後輸出set的大小及内容。
代碼:
#include<iostream>
#include<set>
#include<map>
using namespace std;
map<int, int> couple;
map<int, bool> isArrival;
set<int> single;
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int temp1, temp2;
cin >> temp1 >> temp2;
couple[temp1] = temp2;
couple[temp2] = temp1;
}
int M;
cin >> M;
for (int i = 0; i < M; i++) {
int temp;
cin >> temp;
isArrival[temp] = true;
}
for (map<int, bool>::iterator it = isArrival.begin(); it != isArrival.end(); it++) {
if (isArrival[couple[(*it).first]] == false) {
single.insert((*it).first);
}
}
cout << single.size() << endl;
bool isFirst = true;
for (set<int>::iterator it = single.begin(); it != single.end(); it++) {
if (isFirst == true) {
printf("%05d", (*it));
isFirst = false;
}else {
printf(" %05d", (*it));
}
}
return 0;
}