zoukankan      html  css  js  c++  java
  • 1121 Damn Single (25)

    "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 (<=50000), 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 (<=10000) 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.

    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.

    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

    题目大意:给出n对数,每一对数表示一对情侣的id;再给出m个id进行查询,找出没有情侣,或者情侣没有出现的人,按顺序输出这些人的id。
    思路:1.用数组来记录情侣关系cp[i]=j,cp[j]=i 表示i,j是情侣关系,不存在情侣的记为cp[i]=-1。
    2.记录每一个人的出现与否,exist[i]=1,表示i出现,exis[i]=0 表示i没有出现,将出现的人依次记录在present[i]中
       3.满足条件的为没有情侣的--cp[present[i]]=-1,以及情侣没有出现的--exist[cp[present[i]]]==0;
    要求按id顺序输出,用set来保存满足条件的id,插入过程中,自动排序,也可以保存在数组中,最后再排序
    注意点:当 输入项比较多的时候,应该避免使用cin,cout这两个的效率较低
     1 #include<iostream>
     2 #include<vector>
     3 #include<set>
     4 using namespace std;
     5 int main(){
     6   vector<int> cp(100001,-1), present(100001), exist(100001,0);
     7   set<int> ans;
     8   int n, m, i, a, b, cnt=0;
     9   cin>>n;
    10   for(i=0; i<n; i++){
    11     scanf("%d%d",&a, &b);
    12     cp[a] = b;
    13     cp[b] = a;
    14   }
    15   cin>>m;
    16   for(i=0; i<m; i++){
    17     scanf("%d", &present[i]);
    18     exist[present[i]] = 1;
    19   }
    20   for(i=0; i<m; i++){
    21     if(!(cp[present[i]]!=-1 && exist[cp[present[i]]])){
    22       cnt++;
    23       ans.insert(present[i]);
    24     }
    25   }
    26   cout<<cnt<<endl;
    27   for(auto it=ans.begin(); it!=ans.end(); it++){
    28     if(it==ans.begin())  printf("%05d", *it);
    29     else  printf(" %05d", *it);
    30   }
    31 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    调试php的soapCient
    thinkphp 常见问题
    git使用备忘
    org.apache.catalina.startup.Catalina异常处理
    Java多线程中join方法详解
    WebSphere 安装和配置过程
    数据库错误
    把sql输出成。sql文件
    Oracle 使用命令导入dmp文件
    pl_sql develope连接远程数据库的方法
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9136825.html
Copyright © 2011-2022 走看看