PAT 甲级 刷题日记|A 1121 Damn Single (25 分)

思路

给N对夫妻编号,再给M个派对里的参与人的编号,输出单身的人的编号(包括夫妻没全部到场的也算单身)

代码

#include <bits/stdc++.h>
using namespace std;

unordered_map<int, int> couple;
int n, p;
vector<int> las;
int flag[100002];
vector<int> ans;

int main() {
    cin>>n;
    for (int i = 0; i < 2 * n; i++) {
        int num;
        cin>>num;
        couple[num] = i / 2 + 1;
    }
    cin>>p;
    for (int i = 0; i < p; i++) {
        int peo;
        cin>>peo;
        if (couple.find(peo) == couple.end()) {
            ans.push_back(peo);
        } else {
            las.push_back(peo);
            int n = couple[peo];
            flag[n]++;
        }
    }
    int t = las.size();
    for (int i = 0; i < t; i++) {
        int now = couple[las[i]];
        if (flag[now] == 1)
            ans.push_back(las[i]);
    }
    sort(ans.begin(), ans.end());
    int s = ans.size();
    cout<<s<<endl;
    for (int i = 0; i < s; i++) {
        printf("%05d", ans[i]);
        if (i != s - 1) cout<<" ";
        else cout<<endl;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容