【PAT乙级】1065 单身狗

题目链接:1065 单身狗

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int N, M ,a[100001] = {0}, b[100000], c, d , count = 0, countj = 0;
    cin >> N;
    for(int i=0;i<N;i++){
        cin >> c >> d;
        a[c] = d+1;//写完才发现为0情况会出问题,用+1的办法来修复bug
        a[d] = c+1;
    }
    cin >> M;
    for(int i=0;i<M;i++){
        cin >> c;
        if(!a[c]) b[count++] = c;//单身狗直接输出
        else if(a[a[c]-1]!=-1){//取值时注意-1,伴侣未来暂时入组
            b[count++] = c;
            a[c] = -1;
        }
        else{//伴侣来了在组里找到拉出来
            for(int j=0;j<count;j++){
                if(b[j] == a[c] - 1){
                    b[j] = -1;
                    countj++;//记下拉了几个人出来
                }
            }
        }
    }
    cout << count - countj << endl;
    sort(b,b+count);//给组里的没人陪排下序
    for(int i=countj;i<count;i++){
        printf("%05d",b[i]);
        if(i!=count-1) cout << ' ';
    }
}

猜你喜欢

转载自blog.csdn.net/wulingyu501/article/details/108985303