其他分享
首页 > 其他分享> > PAT (Advanced Level) Practice 1121 Damn Single (25 分) 凌宸1642

PAT (Advanced Level) Practice 1121 Damn Single (25 分) 凌宸1642

作者:互联网

PAT (Advanced Level) Practice 1121 Damn Single (25 分) 凌宸1642

题目描述:

"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 (≤ 50,000), 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 (≤ 10,000) 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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N(≤ 50,000),即夫妻总数。 然后是 N 行情侣,每行给出一对 5 位数字的 ID(即从 00000 到 99999)。 在情侣名单之后,有一个正整数M(≤ 10,000),后面是派对客人的M ID。 数字以空格分隔。 保证没有人有重婚(重婚)或与一个以上的同伴晃来晃去。


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.

译:首先在一行中打印孤独客人的总数。 然后在下一行,按升序打印他们的 ID。 数字必须正好由 1 个空格分隔,并且行尾不得有多余的空格。


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

The Idea:

The Codes:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 100010 ;
map<int , int> cp ;
bool come[maxn] ; 
int n , m , a , b , t ;
vector<int> ans , co ;
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
		cin >> a >> b ;
		cp[a] = b ;      // 记录 a 和 b 互为对象
		cp[b] = a ;
	}
	cin >> m ;
	for(int i = 0 ; i < m ; i ++){
		cin >> t ;
		co.push_back(t) ;
		come[t] = true ;
	}
	for(int i = 0 ; i < co.size() ; i ++){
		if(cp.count(co[i]) == 0) ans.push_back(co[i]) ;  // 没对象 
		else if(come[cp[co[i]]] == false) ans.push_back(co[i]) ; // 对象没来 
	}
	cout << ans.size() << endl ;
	sort(ans.begin() , ans.end()) ;  // 根据要求,按 ID 的非递减序列排序
	for(int i = 0 ; i < ans.size() ; i ++)
		printf("%05d%c" , ans[i] , ((i == ans.size() - 1)?'\n':' ') ) ;
	return 0 ;
} 

标签:25,PAT,Level,int,cin,单身,co,cp,ID
来源: https://www.cnblogs.com/lingchen1642/p/15141880.html