PAT-A1121/B1065 Damn Single/单身狗 题目内容及题解

"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 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 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 递增顺序列出落单的客人。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

解题思路

  1. 读入夫妻状态并记录;
  2. 读入来宾序列,并记录来宾情况;
  3. 对所有来宾检查其单身状况(单身或非单身但独身前来);
  4. 排序并按照题目要求的顺序输出;
  5. 返回零值。

代码

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 100010

int N,M,vis[maxn],couple[maxn],vivid[maxn],seq[maxn],num;
vector<int> Single;

void Init(){
    int a,b;
    scanf("%d",&N);
    while(N--){
        scanf("%d%d",&a,&b);
        couple[a]=b;
        couple[b]=a;
        vivid[a]=1;
        vivid[b]=1;
    }
    scanf("%d",&M);
    while(M--){
        scanf("%d",&a);
        vis[a]=1;
        seq[num++]=a;
    }
}

void Check(int v){
    if(vivid[v]==0){
        Single.push_back(v);
        return;
    }//单身
    if(vis[couple[v]]==0){
        Single.push_back(v);
        return;
    }//独身前来 
}

int main(){
    int i;
    Init();
    for(i=0;i<num;i++){
        Check(seq[i]);
    }
    sort(Single.begin(),Single.end());
    num=Single.size();
    printf("%d\n",num);
    for(i=0;i<num;i++){
        printf("%05d",Single[i]);
        if(i<num-1){
            printf(" ");
        }else{
            printf("\n");
        }
    }
    return 0;
} 

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值